android.os.AsyncTask.get()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(9.3k)|赞(0)|评价(0)|浏览(84)

本文整理了Java中android.os.AsyncTask.get()方法的一些代码示例,展示了AsyncTask.get()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AsyncTask.get()方法的具体详情如下:
包路径:android.os.AsyncTask
类名称:AsyncTask
方法名:get

AsyncTask.get介绍

暂无

代码示例

代码示例来源:origin: robolectric/robolectric

@Test
public void executeOnExecutor_usesPassedExecutor() throws Exception {
 AsyncTask<String, String, String> asyncTask = new MyAsyncTask();
 assertThat(asyncTask.getStatus()).isEqualTo(AsyncTask.Status.PENDING);
 asyncTask.executeOnExecutor(new ImmediateExecutor(), "a", "b");
 assertThat(asyncTask.getStatus()).isEqualTo(AsyncTask.Status.FINISHED);
 assertThat(transcript).containsExactly("onPreExecute", "doInBackground a, b");
 transcript.clear();
 assertEquals("Result should get stored in the AsyncTask", "c", asyncTask.get());
 ShadowLooper.runUiThreadTasks();
 assertThat(transcript).containsExactly("onPostExecute c");
}

代码示例来源:origin: robolectric/robolectric

@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");
}

代码示例来源:origin: robolectric/robolectric

@Test
public void executeReturnsAsyncTask() throws Exception {
 Robolectric.getBackgroundThreadScheduler().unPause();
 AsyncTask<String, String, String> asyncTask = new MyAsyncTask();
 assertThat(asyncTask.execute("a", "b").get()).isEqualTo("c");
}

代码示例来源:origin: robolectric/robolectric

@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"
 );
}

代码示例来源:origin: robolectric/robolectric

@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");
}

代码示例来源:origin: jonfinerty/Once

SharedPreferences get() {
  try {
    return asyncTask.get();
  } catch (InterruptedException | ExecutionException ignored) {
    return null;
  }
}

代码示例来源:origin: ukanth/afwall

/**
 * Runs a script as root (multiple commands separated by "\n")
 *
 * @param ctx    mandatory context
 * @param script the script to be executed
 * @param res    the script output response (stdout + stderr)
 * @return the script exit code
 * @throws IOException on any error executing the script, or writing it to disk
 */
public static int runScriptAsRoot(Context ctx, List<String> script, StringBuilder res) throws IOException {
  int returnCode = -1;
  if ((Looper.myLooper() != null) && (Looper.myLooper() == Looper.getMainLooper())) {
    Log.e(TAG, "runScriptAsRoot should not be called from the main thread\nCall Trace:\n");
    for (StackTraceElement e : new Throwable().getStackTrace()) {
      Log.e(TAG, e.toString());
    }
  }
  try {
    returnCode = new RunCommand().execute(script, res, ctx).get();
  } catch (RejectedExecutionException r) {
    Log.e(TAG, "runScript failed: " + r.getLocalizedMessage());
  } catch (InterruptedException e) {
    Log.e(TAG, "Caught InterruptedException");
  } catch (ExecutionException e) {
    Log.e(TAG, "runScript failed: " + e.getLocalizedMessage());
  } catch (Exception e) {
    Log.e(TAG, "runScript failed: " + e.getLocalizedMessage());
  }
  return returnCode;
}

代码示例来源:origin: aws-amplify/aws-sdk-android

if (imageIconUrl != null) {
  try {
    largeIconBitmap = new DownloadImageTask().execute(imageIconUrl).get();
  } catch (final InterruptedException e) {
    log.error("Interrupted when downloading image : " + e.getMessage(), e);
  if (imageSmallIconUrl != null) {
    try {
      smallIconBitmap = new DownloadImageTask().execute(imageSmallIconUrl).get();
    } catch (final InterruptedException e) {
      log.error("Interrupted when downloading small icon : " + e.getMessage(), e);

代码示例来源:origin: aws-amplify/aws-sdk-android

notificationImage = new DownloadImageTask().execute(imageUrl).get();
} catch (final InterruptedException e) {
  log.error("Interrupted when downloading image : " + e.getMessage(), e);

代码示例来源:origin: derry/delion

/** Immediately returns the phone formatter or null if it has not initialized yet. */
private PhoneNumberFormattingTextWatcher getPhoneFormatter() {
  try {
    return mPhoneFormatterTask.get(0, TimeUnit.MILLISECONDS);
  } catch (CancellationException | ExecutionException | InterruptedException
      | TimeoutException e) {
    return null;
  }
}

代码示例来源:origin: OWASP/SecurityShepherd

public void getKey(View v) {
  JSONParser JSONParse = new JSONParser();
  //JSONParse.execute();
  try {
    Snackbar.make(v, JSONParse.execute().get(), Snackbar.LENGTH_LONG)
        .setAction("Action", null).show();
  } catch (InterruptedException e) {
    e.printStackTrace();
  } catch (ExecutionException e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: derry/delion

/** @return The directory that stores the TabState files. */
@Override
public File getStateDirectory() {
  try {
    return sBaseStateDirectoryFetchTask.get();
  } catch (InterruptedException e) {
  } catch (ExecutionException e) {
  }
  // If the AsyncTask failed for some reason, we have no choice but to fall back to
  // main-thread disk access.
  return ContextUtils.getApplicationContext().getDir(STATE_DIRECTORY, Context.MODE_PRIVATE);
}

代码示例来源:origin: rsiebert/TVHClient

@NonNull
public List<ServerProfile> getRecordingProfiles() {
  List<ServerProfile> serverProfiles = new ArrayList<>();
  try {
    serverProfiles.addAll(new ServerProfileTask(db, RECORDINGS).execute().get());
  } catch (InterruptedException e) {
    Timber.d("Loading recording server profile task got interrupted", e);
  } catch (ExecutionException e) {
    Timber.d("Loading recording server profile task aborted", e);
  }
  return serverProfiles;
}

代码示例来源:origin: rsiebert/TVHClient

@NonNull
public List<ServerProfile> getHttpPlaybackProfiles() {
  List<ServerProfile> serverProfiles = new ArrayList<>();
  try {
    serverProfiles.addAll(new ServerProfileTask(db, HTTP_PLAYBACK).execute().get());
  } catch (InterruptedException e) {
    Timber.d("Loading http playback server profile task got interrupted", e);
  } catch (ExecutionException e) {
    Timber.d("Loading http playback server profile task aborted", e);
  }
  return serverProfiles;
}

代码示例来源:origin: rsiebert/TVHClient

@Override
@Nullable
public ChannelTag getItemById(Object id) {
  try {
    return new ChannelTagByIdTask(db, (int) id).execute().get();
  } catch (InterruptedException e) {
    Timber.d("Loading channel tag by id task got interrupted", e);
  } catch (ExecutionException e) {
    Timber.d("Loading channel tag by id task aborted", e);
  }
  return null;
}

代码示例来源:origin: rsiebert/TVHClient

@Override
@Nullable
public Channel getItemById(Object id) {
  try {
    return new ChannelByIdTask(db, (int) id).execute().get();
  } catch (InterruptedException e) {
    Timber.d("Loading channel by id task got interrupted", e);
  } catch (ExecutionException e) {
    Timber.d("Loading channel by id task aborted", e);
  }
  return null;
}

代码示例来源:origin: rsiebert/TVHClient

@Nullable
public Program getLastItemByChannelId(int channelId) {
  try {
    return new ProgramByIdTask(db, channelId, LOAD_LAST_IN_CHANNEL).execute().get();
  } catch (InterruptedException e) {
    Timber.d("Loading last programs in channel task got interrupted", e);
  } catch (ExecutionException e) {
    Timber.d("Loading last program in channel task aborted", e);
  }
  return null;
}

代码示例来源:origin: rsiebert/TVHClient

public int getItemCount() {
  try {
    return new RecordingCountTask(db).execute().get();
  } catch (InterruptedException e) {
    Timber.d("Loading recording count task got interrupted", e);
  } catch (ExecutionException e) {
    Timber.d("Loading recording count task aborted", e);
  }
  return 0;
}

代码示例来源:origin: rsiebert/TVHClient

@Override
public TimerRecording getItemById(Object id) {
  try {
    return new TimerRecordingByIdTask(db, (String) id).execute().get();
  } catch (InterruptedException e) {
    Timber.d("Loading timer recording by id task got interrupted", e);
  } catch (ExecutionException e) {
    Timber.d("Loading timer recording by id task aborted", e);
  }
  return null;
}

代码示例来源:origin: rsiebert/TVHClient

public int getItemCount() {
  try {
    return new ChannelTagCountTask(db).execute().get();
  } catch (InterruptedException e) {
    Timber.d("Loading channel tag count task got interrupted", e);
  } catch (ExecutionException e) {
    Timber.d("Loading channel tag count task aborted", e);
  }
  return 0;
}

相关文章