@Override protected void onPostExecute(Void result) { super.onPostExecute(result); postLoadData(); } }
@Override protected void onPreExecute() { super.onPreExecute(); mBusy = true; }
public static void postAsync(final Runnable run){ AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>(){ @Override protected String doInBackground(Void... params) { try{ run.run(); }catch(Exception e){ AQUtility.report(e); } return null; } }; task.execute(); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) // API 11 void startMyTask(AsyncTask asyncTask) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params); else asyncTask.execute(params); }
@Override public void onDestroy() { super.onDestroy(); if (mCreateStatisticsOverviewTask != null && !mCreateStatisticsOverviewTask.isCancelled()) { mCreateStatisticsOverviewTask.cancel(true); } } }
private synchronized void cancel() { AsyncTask<?, ?, ?> task = inactivityTask; if (task != null) { task.cancel(true); inactivityTask = null; } }
@Test public void testCancelBeforePostExecute() throws Exception { AsyncTask<String, String, String> asyncTask = new MyAsyncTask(); asyncTask.execute("a", "b"); assertThat(transcript).containsExactly("onPreExecute"); transcript.clear(); ShadowApplication.runBackgroundTasks(); assertThat(transcript).containsExactly("doInBackground a, b"); transcript.clear(); assertEquals("Result should get stored in the AsyncTask", "c", asyncTask.get(100, TimeUnit.MILLISECONDS)); assertFalse(asyncTask.cancel(true)); assertFalse(asyncTask.isCancelled()); ShadowLooper.runUiThreadTasks(); assertThat(transcript).containsExactly("onPostExecute c"); }
@Test public void testCancelBeforeBackground() throws Exception { AsyncTask<String, String, String> asyncTask = new MyAsyncTask(); asyncTask.execute("a", "b"); assertThat(transcript).containsExactly("onPreExecute"); transcript.clear(); assertTrue(asyncTask.cancel(true)); assertTrue(asyncTask.isCancelled()); ShadowApplication.runBackgroundTasks(); assertThat(transcript).isEmpty(); ShadowLooper.runUiThreadTasks(); assertThat(transcript).containsExactly("onCancelled null", "onCancelled"); }
/** * Request the widget to update its status. * TODO Mike - we can reduce battery usage by widget users by removing updatePeriodMillis from metadata * and replacing it with an alarm we set so device doesn't wake to update the widget, see: * https://developer.android.com/guide/topics/appwidgets/#MetaData */ public static void update(Context context) { SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(context); sSmallWidgetEnabled = preferences.getBoolean("widgetSmallEnabled", false); if (sSmallWidgetEnabled && ((sUpdateDeckStatusAsyncTask == null) || (sUpdateDeckStatusAsyncTask.getStatus() == AsyncTask.Status.FINISHED))) { Timber.d("WidgetStatus.update(): updating"); sUpdateDeckStatusAsyncTask = new UpdateDeckStatusAsyncTask(); sUpdateDeckStatusAsyncTask.execute(context); } else { Timber.d("WidgetStatus.update(): already running or not enabled"); } }
@Override protected void onCancelled() { super.onCancelled(); //if (mProgressDialog != null) mProgressDialog.cancel(); }
@Override protected void onProgressUpdate(Progress... values) { if (DEBUG) { MethodLogger.log(); } Threads.checkMainThread(); super.onProgressUpdate(values); }
/** * Called when the scheduling engine has decided to interrupt the execution of a running job, * most likely because the runtime constraints associated with the job are no longer satisfied. * * @return whether the job should be retried * @see Job.Builder#setRetryStrategy(RetryStrategy) * @see RetryStrategy */ @Override public boolean onStopJob(JobParameters jobParameters) { if (mBackgroundTask != null) mBackgroundTask.cancel(true); return true; } }
@Test public void testNormalFlow() throws Exception { AsyncTask<String, String, String> asyncTask = new MyAsyncTask(); asyncTask.execute("a", "b"); assertThat(transcript).containsExactly("onPreExecute"); transcript.clear(); ShadowApplication.runBackgroundTasks(); assertThat(transcript).containsExactly("doInBackground a, b"); transcript.clear(); assertEquals("Result should get stored in the AsyncTask", "c", asyncTask.get(100, TimeUnit.MILLISECONDS)); ShadowLooper.runUiThreadTasks(); assertThat(transcript).containsExactly("onPostExecute c"); }
@Test public void shouldGetStatusForAsyncTask() throws Exception { AsyncTask<String, String, String> asyncTask = new MyAsyncTask(); assertThat(asyncTask.getStatus()).isEqualTo(AsyncTask.Status.PENDING); asyncTask.execute("a"); assertThat(asyncTask.getStatus()).isEqualTo(AsyncTask.Status.RUNNING); Robolectric.getBackgroundThreadScheduler().unPause(); assertThat(asyncTask.getStatus()).isEqualTo(AsyncTask.Status.FINISHED); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override protected final void onCancelled(Void aVoid) { super.onCancelled(aVoid); }
@Override public void onDestroy() { super.onDestroy(); if (mCreateChartTask != null && !mCreateChartTask.isCancelled()) { mCreateChartTask.cancel(true); } } }