@Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); // Restore UI state from the savedInstanceState. // This bundle has also been passed to onCreate. boolean myBoolean = savedInstanceState.getBoolean("MyBoolean"); double myDouble = savedInstanceState.getDouble("myDouble"); int myInt = savedInstanceState.getInt("MyInt"); String myString = savedInstanceState.getString("MyString"); }
@Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); // Save UI state changes to the savedInstanceState. // This bundle will be passed to onCreate if the process is // killed and restarted. savedInstanceState.putBoolean("MyBoolean", true); savedInstanceState.putDouble("myDouble", 1.9); savedInstanceState.putInt("MyInt", 1); savedInstanceState.putString("MyString", "Welcome back to Android"); // etc. }
Bundle extras = getIntent().getExtras(); if (extras != null) { String value = extras.getString("key"); //The key argument here must match that used in the other activity }
private Api() { HandlerThread bgThread = new HandlerThread("api_thread"); bgThread.start(); bgHandler = new Handler(bgThread.getLooper()); mainHandler = new Handler(Looper.getMainLooper()); // Do nothing. }
@SuppressWarnings("WeakerAccess") public BitmapPreFillRunner( BitmapPool bitmapPool, MemoryCache memoryCache, PreFillQueue allocationOrder) { this( bitmapPool, memoryCache, allocationOrder, DEFAULT_CLOCK, new Handler(Looper.getMainLooper())); }
// initialize the progress dialog like in the first example // this is how you fire the downloader mProgressDialog.show(); Intent intent = new Intent(this, DownloadService.class); intent.putExtra("url", "url of the file to download"); intent.putExtra("receiver", new DownloadReceiver(new Handler())); startService(intent);
@Override public void execute(Runnable r) { handler.post(r); } }
private class DownloadReceiver extends ResultReceiver{ public DownloadReceiver(Handler handler) { super(handler); } @Override protected void onReceiveResult(int resultCode, Bundle resultData) { super.onReceiveResult(resultCode, resultData); if (resultCode == DownloadService.UPDATE_PROGRESS) { int progress = resultData.getInt("progress"); mProgressDialog.setProgress(progress); if (progress == 100) { mProgressDialog.dismiss(); } } } }
@Override protected void onSaveInstanceState(Bundle outState) { outState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE"); super.onSaveInstanceState(outState); }
Object getAndroidMainLooperOrNull() { try { return Looper.getMainLooper(); } catch (RuntimeException e) { // Not really a functional Android (e.g. "Stub!" maven dependencies) return null; } }
@Override public boolean isMainThread() { return looper == Looper.myLooper(); }
// Get instance of Vibrator from current Context Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // Start without a delay // Vibrate for 100 milliseconds // Sleep for 1000 milliseconds long[] pattern = {0, 100, 1000}; // The '0' here means to repeat indefinitely // '0' is actually the index at which the pattern keeps repeating from (the start) // To repeat the pattern from any other point, you could increase the index, e.g. '1' v.vibrate(pattern, 0);
private void waitForIdle(final Retryable retryable, final int failedAttempts) { // This needs to be called from the main thread. Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() { @Override public boolean queueIdle() { postToBackgroundWithDelay(retryable, failedAttempts); return false; } }); }
// Get instance of Vibrator from current Context Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // Output yes if can vibrate, no otherwise if (v.hasVibrator()) { Log.v("Can Vibrate", "YES"); } else { Log.v("Can Vibrate", "NO"); }
public AndroidWatchExecutor(long initialDelayMillis) { mainHandler = new Handler(Looper.getMainLooper()); HandlerThread handlerThread = new HandlerThread(LEAK_CANARY_THREAD_NAME); handlerThread.start(); backgroundHandler = new Handler(handlerThread.getLooper()); this.initialDelayMillis = initialDelayMillis; maxBackoffFactor = Long.MAX_VALUE / initialDelayMillis; }
LoadLeaks(DisplayLeakActivity activity, LeakDirectoryProvider leakDirectoryProvider) { this.activityOrNull = activity; this.leakDirectoryProvider = leakDirectoryProvider; mainHandler = new Handler(Looper.getMainLooper()); }
handler = new Handler(); final Runnable r = new Runnable() { public void run() { tv.append("Hello World"); handler.postDelayed(this, 1000); } }; handler.postDelayed(r, 1000);
// Get instance of Vibrator from current Context Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // Start without a delay // Each element then alternates between vibrate, sleep, vibrate, sleep... long[] pattern = {0, 100, 1000, 300, 200, 100, 500, 200, 100}; // The '-1' here means to vibrate once, as '-1' is out of bounds in the pattern array v.vibrate(pattern, -1);
// Get instance of Vibrator from current Context Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // Vibrate for 400 milliseconds v.vibrate(400);