@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); }
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(); }
/** * The entry point to your Job. Implementations should offload work to another thread of * execution as soon as possible. * * This is called by the Job Dispatcher to tell us we should start our job. Keep in mind this * method is run on the application's main thread, so we need to offload work to a background * thread. * * @return whether there is more work remaining. */ @Override public boolean onStartJob(final JobParameters jobParameters) { mFetchWeatherTask = new AsyncTask<Void, Void, Void>(){ @Override protected Void doInBackground(Void... voids) { Context context = getApplicationContext(); SunshineSyncTask.syncWeather(context); jobFinished(jobParameters, false); return null; } @Override protected void onPostExecute(Void aVoid) { jobFinished(jobParameters, false); } }; mFetchWeatherTask.execute(); return true; }
/** * The entry point to your Job. Implementations should offload work to another thread of * execution as soon as possible. * * This is called by the Job Dispatcher to tell us we should start our job. Keep in mind this * method is run on the application's main thread, so we need to offload work to a background * thread. * * @return whether there is more work remaining. */ @Override public boolean onStartJob(final JobParameters jobParameters) { mFetchWeatherTask = new AsyncTask<Void, Void, Void>(){ @Override protected Void doInBackground(Void... voids) { Context context = getApplicationContext(); SunshineSyncTask.syncWeather(context); jobFinished(jobParameters, false); return null; } @Override protected void onPostExecute(Void aVoid) { jobFinished(jobParameters, false); } }; mFetchWeatherTask.execute(); return true; }
/** * The entry point to your Job. Implementations should offload work to another thread of * execution as soon as possible. * * This is called by the Job Dispatcher to tell us we should start our job. Keep in mind this * method is run on the application's main thread, so we need to offload work to a background * thread. * * @return whether there is more work remaining. */ @Override public boolean onStartJob(final JobParameters jobParameters) { mFetchWeatherTask = new AsyncTask<Void, Void, Void>(){ @Override protected Void doInBackground(Void... voids) { Context context = getApplicationContext(); SunshineSyncTask.syncWeather(context); jobFinished(jobParameters, false); return null; } @Override protected void onPostExecute(Void aVoid) { jobFinished(jobParameters, false); } }; mFetchWeatherTask.execute(); return true; }
/** * The entry point to your Job. Implementations should offload work to another thread of * execution as soon as possible. * * This is called by the Job Dispatcher to tell us we should start our job. Keep in mind this * method is run on the application's main thread, so we need to offload work to a background * thread. * * @return whether there is more work remaining. */ @Override public boolean onStartJob(final JobParameters jobParameters) { mFetchWeatherTask = new AsyncTask<Void, Void, Void>(){ @Override protected Void doInBackground(Void... voids) { Context context = getApplicationContext(); SunshineSyncTask.syncWeather(context); jobFinished(jobParameters, false); return null; } @Override protected void onPostExecute(Void aVoid) { jobFinished(jobParameters, false); } }; mFetchWeatherTask.execute(); return true; }
/** * The entry point to your Job. Implementations should offload work to another thread of * execution as soon as possible. * * This is called by the Job Dispatcher to tell us we should start our job. Keep in mind this * method is run on the application's main thread, so we need to offload work to a background * thread. * * @return whether there is more work remaining. */ @Override public boolean onStartJob(final JobParameters jobParameters) { mFetchWeatherTask = new AsyncTask<Void, Void, Void>(){ @Override protected Void doInBackground(Void... voids) { Context context = getApplicationContext(); SunshineSyncTask.syncWeather(context); jobFinished(jobParameters, false); return null; } @Override protected void onPostExecute(Void aVoid) { jobFinished(jobParameters, false); } }; mFetchWeatherTask.execute(); return true; }
/** * The entry point to your Job. Implementations should offload work to another thread of * execution as soon as possible. * * This is called by the Job Dispatcher to tell us we should start our job. Keep in mind this * method is run on the application's main thread, so we need to offload work to a background * thread. * * @return whether there is more work remaining. */ @Override public boolean onStartJob(final JobParameters jobParameters) { mFetchWeatherTask = new AsyncTask<Void, Void, Void>(){ @Override protected Void doInBackground(Void... voids) { Context context = getApplicationContext(); SunshineSyncTask.syncWeather(context); jobFinished(jobParameters, false); return null; } @Override protected void onPostExecute(Void aVoid) { jobFinished(jobParameters, false); } }; mFetchWeatherTask.execute(); return true; }
/** * The entry point to your Job. Implementations should offload work to another thread of * execution as soon as possible. * * This is called by the Job Dispatcher to tell us we should start our job. Keep in mind this * method is run on the application's main thread, so we need to offload work to a background * thread. * * @return whether there is more work remaining. */ @Override public boolean onStartJob(final JobParameters jobParameters) { mFetchWeatherTask = new AsyncTask<Void, Void, Void>(){ @Override protected Void doInBackground(Void... voids) { Context context = getApplicationContext(); SunshineSyncTask.syncWeather(context); jobFinished(jobParameters, false); return null; } @Override protected void onPostExecute(Void aVoid) { jobFinished(jobParameters, false); } }; mFetchWeatherTask.execute(); return true; }
/** * The entry point to your Job. Implementations should offload work to another thread of * execution as soon as possible. * * This is called by the Job Dispatcher to tell us we should start our job. Keep in mind this * method is run on the application's main thread, so we need to offload work to a background * thread. * * @return whether there is more work remaining. */ @Override public boolean onStartJob(final JobParameters jobParameters) { mFetchWeatherTask = new AsyncTask<Void, Void, Void>(){ @Override protected Void doInBackground(Void... voids) { Context context = getApplicationContext(); SunshineSyncTask.syncWeather(context); jobFinished(jobParameters, false); return null; } @Override protected void onPostExecute(Void aVoid) { jobFinished(jobParameters, false); } }; mFetchWeatherTask.execute(); return true; }
/** * The entry point to your Job. Implementations should offload work to another thread of * execution as soon as possible. * * This is called by the Job Dispatcher to tell us we should start our job. Keep in mind this * method is run on the application's main thread, so we need to offload work to a background * thread. * * @return whether there is more work remaining. */ @Override public boolean onStartJob(final JobParameters jobParameters) { mFetchWeatherTask = new AsyncTask<Void, Void, Void>(){ @Override protected Void doInBackground(Void... voids) { Context context = getApplicationContext(); SunshineSyncTask.syncWeather(context); jobFinished(jobParameters, false); return null; } @Override protected void onPostExecute(Void aVoid) { jobFinished(jobParameters, false); } }; mFetchWeatherTask.execute(); return true; }
info.iconLoadTask.execute();
/** * 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"); } }
@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 executeReturnsAsyncTask() throws Exception { Robolectric.getBackgroundThreadScheduler().unPause(); AsyncTask<String, String, String> asyncTask = new MyAsyncTask(); assertThat(asyncTask.execute("a", "b").get()).isEqualTo("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"); }
@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); }
@Test public void onPostExecute_doesNotSwallowExceptions() throws Exception { Robolectric.getBackgroundThreadScheduler().unPause(); Robolectric.getForegroundThreadScheduler().unPause(); AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { return null; } @Override protected void onPostExecute(Void aVoid) { throw new RuntimeException("Don't swallow me!"); } }; try { asyncTask.execute(); fail("Task swallowed onPostExecute() exception!"); } catch (RuntimeException e) { assertThat(e.getCause().getMessage()).isEqualTo("Don't swallow me!"); } }
@Test public void progressUpdatesAreQueuedUntilBackgroundThreadFinishes() throws Exception { AsyncTask<String, String, String> asyncTask = new MyAsyncTask() { @Override protected String doInBackground(String... strings) { publishProgress("33%"); publishProgress("66%"); publishProgress("99%"); return "done"; } }; asyncTask.execute("a", "b"); assertThat(transcript).containsExactly("onPreExecute"); transcript.clear(); ShadowApplication.runBackgroundTasks(); assertThat(transcript).isEmpty(); assertEquals("Result should get stored in the AsyncTask", "done", asyncTask.get(100, TimeUnit.MILLISECONDS)); ShadowLooper.runUiThreadTasks(); assertThat(transcript).containsExactly( "onProgressUpdate 33%", "onProgressUpdate 66%", "onProgressUpdate 99%", "onPostExecute done" ); }
@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"); }