@Override public void onProfileDone() { ((Activity) mContext).runOnUiThread(new Runnable() { @Override public void run() { new ProfileResultDialog( new ContextThemeWrapper(mContext, BaseDialog.getDialogTheme(mContext)), profileProgressbar.getSamples()).show(); closeProgressbar(); } }); }
@Override public void onProgress(final int step) { ((Activity) mContext).runOnUiThread(new Runnable() { @Override public void run() { profileProgressbar.setProgress(step); } }); }
public void setAlias(String alias) { // Note: KeyChainAliasCallback gives back "" on cancel if (alias != null && alias.equals("")) { alias = null; } mAlias = alias; // Note: KeyChainAliasCallback is a different thread than the UI mActivity.runOnUiThread(new Runnable() { @Override public void run() { updateView(); if (mListener != null) { mListener.onClientCertificateChanged(mAlias); } } }); }
/** * Run the operation of loading a bitmap on the UI thread. * * @param bitmap The image to load. */ private void loadBitmap(final Bitmap bitmap) { ((Activity) getContext()).runOnUiThread(new Runnable() { @Override public void run() { AutoLoadImageView.this.setImageBitmap(bitmap); } }); }
/** * Loads the image place holder if any has been assigned. */ private void loadImagePlaceHolder() { if (this.imagePlaceHolderResId != -1) { ((Activity) getContext()).runOnUiThread(new Runnable() { @Override public void run() { AutoLoadImageView.this.setImageResource( AutoLoadImageView.this.imagePlaceHolderResId); } }); } }
@Override public void onError(final String message, Exception e) { Log.e(getClass().getSimpleName(), message, e); getActivity().runOnUiThread(new Runnable() { @Override public void run() { Toast .makeText(getActivity(), message, Toast.LENGTH_LONG) .show(); } }); }
@Override public void run() { final FetchedAppSettings settings = FetchedAppSettingsManager.queryAppSettings(appId, false); getActivity().runOnUiThread(new Runnable() { @Override public void run() { showToolTipPerSettings(settings); } }); } });
/** 隐藏加载进度 */ public static void dismissProgressDialog(Activity context) { if(context == null || progressDialog == null || progressDialog.isShowing() == false){ return; } context.runOnUiThread(new Runnable() { @Override public void run() { progressDialog.dismiss(); } }); } //显示与关闭进度弹窗方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/** 隐藏加载进度 */ public static void dismissProgressDialog(Activity context) { if(context == null || progressDialog == null || progressDialog.isShowing() == false){ return; } context.runOnUiThread(new Runnable() { @Override public void run() { progressDialog.dismiss(); } }); } //显示与关闭进度弹窗方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
@Override public void onReceive(final Context context, final Intent intent) { if(context instanceof Activity) { ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { notifyIntent(context, intent); } }); } else { notifyIntent(context, intent); } }
/** * Sets the progress of a given {@link ProgressBar}. Examples are SeekBar and RatingBar. * @param progressBar the {@code ProgressBar} * @param progress the progress that the {@code ProgressBar} should be set to */ public void setProgressBar(final ProgressBar progressBar,final int progress) { if(progressBar != null){ Activity activity = activityUtils.getCurrentActivity(false); if(activity != null){ activity.runOnUiThread(new Runnable() { public void run() { try{ progressBar.setProgress(progress); }catch (Exception ignored){} } }); } } }
/** * Shows a {@link Toast} on the UI thread. * * @param text The message to show */ private void showToast(final String text) { final Activity activity = getActivity(); if (activity != null) { activity.runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(activity, text, Toast.LENGTH_SHORT).show(); } }); } }
public void removeSplashScreen() { logger.log(Level.FINE, "Splash Screen Picture Resource ID: {0}", splashPicID); if (splashPicID != 0) { if (frameLayout != null) { if (splashImageView != null) { getActivity().runOnUiThread(new Runnable() { @Override public void run() { splashImageView.setVisibility(View.INVISIBLE); frameLayout.removeView(splashImageView); } }); } else { logger.fine("splashImageView is null"); } } else { logger.fine("frameLayout is null"); } } }
/** * Sets the date in a given {@link DatePicker}. * * @param datePicker the {@code DatePicker} object. * @param year the year e.g. 2011 * @param monthOfYear the month which is starting from zero e.g. 03 * @param dayOfMonth the day e.g. 10 */ public void setDatePicker(final DatePicker datePicker, final int year, final int monthOfYear, final int dayOfMonth) { if(datePicker != null){ Activity activity = activityUtils.getCurrentActivity(false); if(activity != null){ activity.runOnUiThread(new Runnable() { public void run() { try{ datePicker.updateDate(year, monthOfYear, dayOfMonth); }catch (Exception ignored){} } }); } } }
public void doScreenshot() { View v = getScreenshotView(); if(v == null) keepRunning = false; String final_name = name+"_"+seqno; ScreenshotRunnable r = new ScreenshotRunnable(v, final_name, quality); Log.d(LOG_TAG, "taking screenshot "+final_name); Activity activity = activityUtils.getCurrentActivity(false); if(activity != null){ activity.runOnUiThread(r); } else { instrumentation.runOnMainSync(r); } }
@Subscribe public void onRandomEvent(final RandomEvent event) { if (getActivity() != null) { getActivity().runOnUiThread(new Runnable() { @Override public void run() { adapter.add(event); } }); } }
@Override public void onMenuClick(Activity activity, final MenuItem item) { if(isRunning()) { stop(); } else { start(); } activity.runOnUiThread(new Runnable() { @Override public void run() { buildMenuItem(item); } }); }
public void finishLastActivity(@Nullable Thread uncaughtExceptionThread) { // Trying to solve https://github.com/ACRA/acra/issues/42#issuecomment-12134144 // Determine the current/last Activity that was started and close // it. Activity#finish (and maybe it's parent too). final Activity lastActivity = lastActivityManager.getLastActivity(); if (lastActivity != null) { final boolean isMainThread = uncaughtExceptionThread == lastActivity.getMainLooper().getThread(); if (ACRA.DEV_LOGGING) ACRA.log.d(LOG_TAG, "Finishing the last Activity prior to killing the Process"); final Runnable finisher = () -> { lastActivity.finish(); if (ACRA.DEV_LOGGING) ACRA.log.d(LOG_TAG, "Finished " + lastActivity.getClass()); }; if (isMainThread) { finisher.run(); } else { lastActivity.runOnUiThread(finisher); } // A crashed activity won't continue its lifecycle. So we only wait if something else crashed if (!isMainThread) { lastActivityManager.waitForActivityStop(100); } lastActivityManager.clearLastActivity(); } }
@Test public void shouldRunUiTasksImmediatelyByDefault() throws Exception { TestRunnable runnable = new TestRunnable(); activity = Robolectric.setupActivity(DialogLifeCycleActivity.class); activity.runOnUiThread(runnable); assertTrue(runnable.wasRun); }
@Test public void shouldQueueUiTasksWhenUiThreadIsPaused() throws Exception { ShadowLooper.pauseMainLooper(); activity = Robolectric.setupActivity(DialogLifeCycleActivity.class); TestRunnable runnable = new TestRunnable(); activity.runOnUiThread(runnable); assertFalse(runnable.wasRun); ShadowLooper.unPauseMainLooper(); assertTrue(runnable.wasRun); }