android.os.Handler#hasMessages ( )源码实例Demo

下面列出了android.os.Handler#hasMessages ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: android_9.0.0_r45   文件: GLThreadManager.java
/**
 * 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!");
    }
}
 
源代码2 项目: android_9.0.0_r45   文件: QueuedWork.java
/**
 * 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: ");
            }
        }
    }
}
 
源代码3 项目: ArgusAPM   文件: DbCache.java
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;
            }
        }
    };
}