Java Code Examples for android.os.Handler#hasMessages()

The following examples show how to use android.os.Handler#hasMessages() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GLThreadManager.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Queue a new call to draw into the surfaces specified in the next available preview
 * request from the {@link CaptureCollector} passed to
 * {@link #setConfigurationAndWait(java.util.Collection, CaptureCollector)};
 */
public void queueNewFrame() {
    Handler handler = mGLHandlerThread.getHandler();

    /**
     * Avoid queuing more than one new frame.  If we are not consuming faster than frames
     * are produced, drop frames rather than allowing the queue to back up.
     */
    if (!handler.hasMessages(MSG_NEW_FRAME)) {
        handler.sendMessage(handler.obtainMessage(MSG_NEW_FRAME));
    } else {
        Log.e(TAG, "GLThread dropping frame.  Not consuming frames quickly enough!");
    }
}
 
Example 2
Source File: QueuedWork.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Trigger queued work to be processed immediately. The queued work is processed on a separate
 * thread asynchronous. While doing that run and process all finishers on this thread. The
 * finishers can be implemented in a way to check weather the queued work is finished.
 *
 * Is called from the Activity base class's onPause(), after BroadcastReceiver's onReceive,
 * after Service command handling, etc. (so async work is never lost)
 */
public static void waitToFinish() {
    long startTime = System.currentTimeMillis();
    boolean hadMessages = false;

    Handler handler = getHandler();

    synchronized (sLock) {
        if (handler.hasMessages(QueuedWorkHandler.MSG_RUN)) {
            // Delayed work will be processed at processPendingWork() below
            handler.removeMessages(QueuedWorkHandler.MSG_RUN);

            if (DEBUG) {
                hadMessages = true;
                Log.d(LOG_TAG, "waiting");
            }
        }

        // We should not delay any work as this might delay the finishers
        sCanDelay = false;
    }

    StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
    try {
        processPendingWork();
    } finally {
        StrictMode.setThreadPolicy(oldPolicy);
    }

    try {
        while (true) {
            Runnable finisher;

            synchronized (sLock) {
                finisher = sFinishers.poll();
            }

            if (finisher == null) {
                break;
            }

            finisher.run();
        }
    } finally {
        sCanDelay = true;
    }

    synchronized (sLock) {
        long waitTime = System.currentTimeMillis() - startTime;

        if (waitTime > 0 || hadMessages) {
            mWaitTimes.add(Long.valueOf(waitTime).intValue());
            mNumWaits++;

            if (DEBUG || mNumWaits % 1024 == 0 || waitTime > MAX_WAIT_TIME_MILLIS) {
                mWaitTimes.log(LOG_TAG, "waited: ");
            }
        }
    }
}
 
Example 3
Source File: DbCache.java    From ArgusAPM with Apache License 2.0 4 votes vote down vote up
private void newThread() {
    HandlerThread handlerThread = new HandlerThread("dbCache");
    handlerThread.start();
    mHandler = new Handler(handlerThread.getLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_WHAT_PRE_WRITE_DB:
                    InfoHolder holder = null;
                    try {
                        holder = (InfoHolder) msg.obj;
                    } catch (ClassCastException ex) {
                        LogX.d(Env.TAG, SUB_TAG, "class cast exception : " + ex.getMessage());
                    }
                    if (holder != null) {
                        handleData(holder);
                    } else {
                        if (Env.DEBUG) {
                            LogX.d(Env.TAG, SUB_TAG, "holder == null");
                        }
                    }
                    break;
                case MSG_WHAT_WRITE_DB:
                    if (Env.DEBUG) {
                        LogX.d(Env.TAG, SUB_TAG, "MSG_WHAT_WRITE_DB");
                    }
                    updateTime(System.currentTimeMillis());
                    readFromListAndWriteToDB();
                    break;
                case MSG_WHAT_TIME_OUT:
                    if (Env.DEBUG) {
                        LogX.d(Env.TAG, SUB_TAG, "MSG_WHAT_TIME_OUT -> MSG_WHAT_WRITE_DB");
                    }
                    if (mHandler.hasMessages(MSG_WHAT_WRITE_DB)) {
                        mHandler.removeMessages(MSG_WHAT_WRITE_DB);
                    }
                    mHandler.sendEmptyMessage(MSG_WHAT_WRITE_DB);
                    break;
            }
        }
    };
}