private synchronized void cancel() { AsyncTask<?, ?, ?> task = inactivityTask; if (task != null) { task.cancel(true); inactivityTask = null; } }
/** * 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; } }
/** * 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; } }
@Override public void onDestroy() { if (task!=null) { task.cancel(false); } current.close(); db.close(); super.onDestroy(); }
@Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); for (ActivityInfo activityInfo : adapter.activityInfos) { if (activityInfo.iconLoadTask != null) { activityInfo.iconLoadTask.cancel(true); activityInfo.iconLoadTask = null; } } }
private synchronized void cancelOutstandingTask() { if (outstandingTask != null) { if (outstandingTask.getStatus() != AsyncTask.Status.FINISHED) { outstandingTask.cancel(true); } outstandingTask = null; } }
@Override public void onPause() { super.onPause(); task.cancel(true); }
/** * Gently killing AsyncTask */ public static void stopTaskGracefully(AsyncTask<?, ?, ?> t) { if (t != null) { if (t.getStatus() == Status.RUNNING) { t.cancel(true); } } }
@Override protected void onDestroy() { if (mSearchTask != null && mSearchTask.getStatus() != AsyncTask.Status.FINISHED) { mSearchTask.cancel(false); } super.onDestroy(); }
@Override public void onDestroy() { super.onDestroy(); if (mCreateStatisticsOverviewTask != null && !mCreateStatisticsOverviewTask.isCancelled()) { mCreateStatisticsOverviewTask.cancel(true); } } }
@Override public void onDestroy() { if (task!=null) { task.cancel(false); } ((CursorAdapter)getListAdapter()).getCursor().close(); super.onDestroy(); }
@Override public void onDestroy() { super.onDestroy(); if (mCreateChartTask != null && !mCreateChartTask.isCancelled()) { mCreateChartTask.cancel(true); } } }
@Override public boolean onQueryTextChange(final String newText) { if (newText.equals(queryString)) { return true; } if (mSearchTask != null) { mSearchTask.cancel(false); mSearchTask = null; } queryString = newText; if (queryString.trim().equals("")) { searchResults.clear(); adapter.updateSearchResults(searchResults); adapter.notifyDataSetChanged(); } else { mSearchTask = new SearchTask().executeOnExecutor(mSearchExecutor, queryString); } return true; }
@Override public void onDestroy() { if (task != null) { task.cancel(false); } ((CursorAdapter)getListAdapter()).getCursor().close(); db.close(); super.onDestroy(); }
@Override public void onDestroy() { if (task != null) { task.cancel(false); } ((CursorAdapter)getListAdapter()).getCursor().close(); db.close(); super.onDestroy(); }
@Override public void onDestroy() { if (task!=null) { task.cancel(false); } ((CursorAdapter)getListAdapter()).getCursor().close(); db.close(); super.onDestroy(); }
@Override public void onDestroy() { if (task!=null) { task.cancel(false); } ((CursorAdapter)getListAdapter()).getCursor().close(); db.close(); super.onDestroy(); }
@Override public void onDestroy() { if (task != null) { task.cancel(false); } ((CursorAdapter)getListAdapter()).getCursor().close(); db.close(); super.onDestroy(); }
@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"); }
@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"); }