/** * Prior to Android 5, HandlerThread always keeps a stack local reference to the last message * that was sent to it. This method makes sure that stack local reference never stays there * for too long by sending new messages to it every second. */ static void flushStackLocalLeaks(Looper looper) { Handler handler = new Handler(looper) { @Override public void handleMessage(Message msg) { sendMessageDelayed(obtainMessage(), THREAD_LEAK_CLEANING_MS); } }; handler.sendMessageDelayed(handler.obtainMessage(), THREAD_LEAK_CLEANING_MS); }
void dispatchRetry(BitmapHunter hunter) { handler.sendMessageDelayed(handler.obtainMessage(HUNTER_RETRY, hunter), RETRY_DELAY); }
public void postEventDelayed(MediaEvent e, long delay) { Message msg = handler.obtainMessage(MEDIA_EVENT, e); handler.sendMessageDelayed(msg, delay); }
public void onAutoFocus(boolean success, Camera camera) { if (autoFocusHandler != null) { Message message = autoFocusHandler.obtainMessage(autoFocusMessage, success); autoFocusHandler.sendMessageDelayed(message, AUTOFOCUS_INTERVAL_MS); autoFocusHandler = null; } else { Log.d(TAG, "Got auto-focus callback, but no handler for it"); } }
public void onAutoFocus(boolean success, Camera camera) { if (autoFocusHandler != null) { Message message = autoFocusHandler.obtainMessage(autoFocusMessage, success); autoFocusHandler.sendMessageDelayed(message, AUTOFOCUS_INTERVAL_MS); autoFocusHandler = null; } else { Log.d(TAG, "Got auto-focus callback, but no handler for it"); } }
public void onAutoFocus(boolean success, Camera camera) { if (autoFocusHandler != null) { Message message = autoFocusHandler.obtainMessage(autoFocusMessage, success); autoFocusHandler.sendMessageDelayed(message, AUTOFOCUS_INTERVAL_MS); autoFocusHandler = null; } else { Log.d(TAG, "Got auto-focus callback, but no handler for it"); } }
/** * Enqueue a message containing what value and object into the message queue after all pending messages before (current time + delayMillis). * * @param handler * @param what * @param obj * @param delayTime */ public static void sendMessageHandlerDelay(Handler handler, int what, Object obj, long delayTime) { Message message = new Message(); message.what = what; message.obj = obj; handler.sendMessageDelayed(message, delayTime); }
private static void acquireWakeLockAndSendMessage(Context context, Message msg, long delay) { if (mWakeLock == null) { Context appContext = context.getApplicationContext(); PowerManager pm = (PowerManager) appContext.getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Timber headset button"); mWakeLock.setReferenceCounted(false); } if (DEBUG) Log.v(TAG, "Acquiring wake lock and sending " + msg.what); // Make sure we don't indefinitely hold the wake lock under any circumstances mWakeLock.acquire(10000); mHandler.sendMessageDelayed(msg, delay); }
private static void acquireWakeLockAndSendMessage(Context context, Message msg, long delay) { if (mWakeLock == null) { Context appContext = context.getApplicationContext(); PowerManager pm = (PowerManager) appContext.getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Timber headset button"); mWakeLock.setReferenceCounted(false); } if (DEBUG) Log.v(TAG, "Acquiring wake lock and sending " + msg.what); // Make sure we don't indefinitely hold the wake lock under any circumstances mWakeLock.acquire(10000); mHandler.sendMessageDelayed(msg, delay); }
private synchronized void autoFocusAgainLater() { if (!stopped && !handler.hasMessages(MESSAGE_FOCUS)) { handler.sendMessageDelayed(handler.obtainMessage(MESSAGE_FOCUS), AUTO_FOCUS_INTERVAL_MS); } }
/** * Enqueue a message into the message queue after all pending messages before (current time + delayMillis). * The message contains what value and a bundle with key and a String value. * @param handler * @param what * @param key * @param value * @param delayTime */ public static void sendMessageHandlerDelay(Handler handler, int what, String key, String value, long delayTime) { Message message = new Message(); message.what = what; Bundle bundle = new Bundle(); bundle.putString(key, value); message.setData(bundle); // handler.sendMessage(message); handler.sendMessageDelayed(message, delayTime); }
@Override public void onTextChanged(CharSequence s, int start, int before, int count) { inputedString = StringUtil.getTrimedString(s); if (StringUtil.isNotEmpty(inputedString, true) == false) { ivEditTextInfoClear.setVisibility(View.GONE); } else { ivEditTextInfoClear.setVisibility(View.VISIBLE); if (hasUrl == true) { Message msg = new Message(); msg.obj = inputedString; searchHandler.sendMessageDelayed(msg , SEARCH_DELAY_TIME); } } } @Override
@Override public void onTextChanged(CharSequence s, int start, int before, int count) { inputedString = StringUtil.getTrimedString(s); if (StringUtil.isNotEmpty(inputedString, true) == false) { ivEditTextInfoClear.setVisibility(View.GONE); } else { ivEditTextInfoClear.setVisibility(View.VISIBLE); if (hasUrl == true) { Message msg = new Message(); msg.obj = inputedString; searchHandler.sendMessageDelayed(msg , SEARCH_DELAY_TIME); } } } @Override
void performSync(@NonNull Job job) { // assume that connection wouldn't change until we finish syncing mJob = job; if (!TextUtils.isEmpty(mJob.id)) { Message message = Message.obtain(mHandler, this::stopSync); message.what = Integer.valueOf(mJob.id); mHandler.sendMessageDelayed(message, TIMEOUT_MILLIS); mSyncProgress = new SyncProgress(mJob); sync(mJob.id); } else { syncDeferredItems(); } }
@Override @SuppressLint("NewApi") // Async will only be true when the API is available to call. public Disposable schedule(Runnable run, long delay, TimeUnit unit) { if (run == null) throw new NullPointerException("run == null"); if (unit == null) throw new NullPointerException("unit == null"); if (disposed) { return Disposables.disposed(); } run = RxJavaPlugins.onSchedule(run); ScheduledRunnable scheduled = new ScheduledRunnable(handler, run); Message message = Message.obtain(handler, scheduled); message.obj = this; // Used as token for batch disposal of this worker's runnables. if (async) { message.setAsynchronous(true); } handler.sendMessageDelayed(message, unit.toMillis(delay)); // Re-check disposed state for removing in case we were racing a call to dispose(). if (disposed) { handler.removeCallbacks(scheduled); return Disposables.disposed(); } return scheduled; }
@Override public boolean handleMessage(Message msg) { int currentLength = getText().length(); if (currentLength < mText.length()) { if (currentLength + charIncrease > mText.length()) { charIncrease = mText.length() - currentLength; } append(mText.subSequence(currentLength, currentLength + charIncrease)); long randomTime = typerSpeed + random.nextInt(typerSpeed); Message message = Message.obtain(); message.what = INVALIDATE; handler.sendMessageDelayed(message, randomTime); return false; } else { if(animationListener != null) { animationListener.onAnimationEnd(TyperTextView.this); } } return false; } });
private void push() { final int delayMillis; synchronized (queueLock) { if (!disposingList.isEmpty()) { // is disposing. return; } if (waitingQueue.isEmpty()) { // not messenger need be handled. return; } if (!isIntervalValid()) { waitingQueue.drainTo(disposingList); delayMillis = 0; } else { delayMillis = INTERVAL; final int size = Math.min(waitingQueue.size(), SUB_PACKAGE_SIZE); for (int i = 0; i < size; i++) { disposingList.add(waitingQueue.remove()); } } } handler.sendMessageDelayed(handler.obtainMessage(DISPOSE_MESSENGER_LIST, disposingList), delayMillis); }
@Override public boolean onError(final MediaPlayer mp, final int what, final int extra) { Log.w(TAG, "Music Server Error what: " + what + " extra: " + extra); switch (what) { case MediaPlayer.MEDIA_ERROR_SERVER_DIED: final MusicService service = mService.get(); final TrackErrorInfo errorInfo = new TrackErrorInfo(service.getAudioId(), service.getTrackName()); mIsInitialized = false; mCurrentMediaPlayer.release(); mCurrentMediaPlayer = new MediaPlayer(); mCurrentMediaPlayer.setWakeMode(service, PowerManager.PARTIAL_WAKE_LOCK); Message msg = mHandler.obtainMessage(SERVER_DIED, errorInfo); mHandler.sendMessageDelayed(msg, 2000); return true; default: break; } return false; }
@Override public boolean onError(final MediaPlayer mp, final int what, final int extra) { Log.w(TAG, "Music Server Error what: " + what + " extra: " + extra); switch (what) { case MediaPlayer.MEDIA_ERROR_SERVER_DIED: final MediaService service = mService.get(); final TrackErrorInfo errorInfo = new TrackErrorInfo(service.getAudioId(), service.getTrackName()); mIsInitialized = false; mIsTrackPrepared = false; mCurrentMediaPlayer.release(); mCurrentMediaPlayer = new MediaPlayer(); mCurrentMediaPlayer.setWakeMode(service, PowerManager.PARTIAL_WAKE_LOCK); Message msg = mHandler.obtainMessage(SERVER_DIED, errorInfo); mHandler.sendMessageDelayed(msg, 2000); return true; default: break; } return false; }
@Test public void scheduler_wontDispatchRemovedMessage_evenIfMessageReused() { final ArrayList<Long> runAt = new ArrayList<>(); ShadowLooper.pauseMainLooper(); Handler handler = new Handler() { @Override public void handleMessage(Message msg) { runAt.add(shadowOf(Looper.myLooper()).getScheduler().getCurrentTime()); } }; final long startTime = Robolectric.getForegroundThreadScheduler().getCurrentTime(); Message msg = handler.obtainMessage(123); handler.sendMessageDelayed(msg, 200); handler.removeMessages(123); Message newMsg = handler.obtainMessage(123); assertThat(newMsg).named("new message").isSameAs(msg); handler.sendMessageDelayed(newMsg, 400); ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); // Original implementation had a bug which caused reused messages to still // be invoked at their original post time. assertThat(runAt).named("handledAt").containsExactly(startTime + 400L); }