These code examples were ranked by Codota’s semantic indexing as the best open source examples for Handler post method.
final Handler handler = new Handler(); Timer t = new Timer(); t.schedule(new TimerTask() { @Override public void run() { handler.post(new Runnable() { public void run() { // This will kill the parent, a bad idea. // parent.finish(); // This will start the browser, a better idea launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/"))); } }); } }, DELAY_MS); } }
if (DEBUG) Slog.d(TAG, String.format( "Startup with UMS connection %s (media state %s)", mUmsAvailable, Environment.getExternalStorageState())); HandlerThread thr = new HandlerThread("SystemUI StorageNotification"); thr.start(); mAsyncEventHandler = new Handler(thr.getLooper()); onUsbMassStorageConnectionChanged(connected); } /* * @override com.android.os.storage.StorageEventListener */ @Override public void onUsbMassStorageConnectionChanged(final boolean connected) { mAsyncEventHandler.post(new Runnable() { @Override public void run() { onUsbMassStorageConnectionChangedAsync(connected); } });
* @param itemId a string identifying the item * @param quantity the quantity of items in this purchase */ void postPurchaseStateChange(final PurchaseState purchaseState, final String itemId, final int quantity, final long purchaseTime, final String developerPayload) { mHandler.post(new Runnable() { @Override public void run() { onPurchaseStateChange( purchaseState, itemId, quantity, purchaseTime, developerPayload); } }); } }
} public PicasaService() { super(); mSyncThread.start(); mSyncHandler = new Handler(mSyncThread.getLooper()); mSyncHandler.post(new Runnable() { public void run() { Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); } }); } private static PicasaContentProvider getContentProvider(Context context) { ContentResolver cr = context.getContentResolver(); ContentProviderClient client = cr.acquireContentProviderClient(PicasaContentProvider.AUTHORITY); return (PicasaContentProvider) client.getLocalContentProvider(); } @Override
@Override public void onActivityResult(final int requestCode, final int resultCode, final Intent data) { super.onActivityResult(requestCode, resultCode, data); trackLifecycleEvent("onActivityResult"); Handler handler = new Handler(); handler.post(new Runnable() { @Override public void run() { onActivityResultUI(requestCode, resultCode, data); } }); } public void onActivityResultUI(int requestCode, int resultCode, Intent data) { trackLifecycleEvent("onActivityResultUI"); }
* @param c The Callable to run * @return The result of the callable * @throws ExecutionException c's exception */ public static <T> T runOnUiThreadBlocking(Callable<T> c) throws ExecutionException { FutureTask<T> task = new FutureTask<T>(c); runOnUiThread(task); try { return task.get(); } catch (InterruptedException e) { throw new RuntimeException("Interrupted waiting for callable", e); } } /** * Run the supplied FutureTask on the main thread. The method will block only if the current * thread is the main thread. * * @param task The FutureTask to run * @return The queried task (to aid inline construction)
public static <T> T callInMainThread(Callable<T> call) throws Exception { if (isMainThread()) { return call.call(); } else { FutureTask<T> task = new FutureTask<T>(call); getHandler().post(task); return task.get(); } } public static Bitmap takeScreenshot(View view, Bitmap.Config config) { int width = view.getWidth(); int height = view.getHeight(); if (view != null && width > 0 && height > 0) { Bitmap bitmap = Bitmap.createBitmap(width, height, config); Canvas canvas = new Canvas(bitmap); view.draw(canvas); return bitmap;
@Override public void run() { final Bitmap bitmap = getThumbnail(maxWidth, maxHeight); final ImageView destination = weakImageView.get(); if (destination != null && destination.getDrawable() == temporaryDrawable) { handler.post(new Runnable() { @Override public void run() { setThumbnailOn(destination, bitmap, false); } }); } } }); } private void setThumbnailOn(ImageView imageView, Bitmap thumbnail, boolean fromMemory) { if (fromMemory) { imageView.setImageBitmap(thumbnail); } else {