寻找在 android 中使用 get() 和 AsyncTask 的好例子

发布于 2024-11-29 16:55:38 字数 301 浏览 2 评论 0原文

我对 AsyncTask 中的 get(long, java.util.concurrent.TimeUnit) 函数很好奇,但我很难找到它的用法示例。

get(long, java.util.concurrent.TimeUnit)

任何人都可以提供它的使用示例吗?

I'm curious about the get(long, java.util.concurrent.TimeUnit) function in AsyncTask, but I'm having a hard time locating an example of it's usage.

get(long, java.util.concurrent.TimeUnit)

Can anyone provide an example of it's use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦初醒的夏天 2024-12-06 16:55:38

看起来就像 AsyncTask。 get() 阻塞调用者线程,其中 AsyncTask.execute() 确实不是。

您可能想要使用 AsyncTask.get() 来测试特定的 Web 服务调用,但您不需要它是异步的,并且您希望控制它需要多长时间来完成。或者任何时候您想在测试套件中测试您的 Web 服务。

语法与执行相同:

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
 protected Long doInBackground(URL... urls) {
     int count = urls.length;
     long totalSize = 0;
     for (int i = 0; i < count; i++) {
         totalSize += Downloader.downloadFile(urls[i]);
         publishProgress((int) ((i / (float) count) * 100));
     }
     return totalSize;
 }

 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(Long result) {
     showDialog("Downloaded " + result + " bytes");
 }
}

new DownloadFilesTask().get(5000, TimeUnit.MILLISECONDS);

It appears as though AsyncTask.get() blocks the caller thread, where AsyncTask.execute() does not.

You might want to use AsyncTask.get() for test cases where you want to test a particular Web Service call, but you do not need it to be asynchronous and you would like to control how long it takes to complete. Or any time you would like to test against your web service in a test suite.

Syntax is the same as execute:

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
 protected Long doInBackground(URL... urls) {
     int count = urls.length;
     long totalSize = 0;
     for (int i = 0; i < count; i++) {
         totalSize += Downloader.downloadFile(urls[i]);
         publishProgress((int) ((i / (float) count) * 100));
     }
     return totalSize;
 }

 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(Long result) {
     showDialog("Downloaded " + result + " bytes");
 }
}

new DownloadFilesTask().get(5000, TimeUnit.MILLISECONDS);
兮颜 2024-12-06 16:55:38

AsyncTask 的另一个用途是了解多个 AsyncTask 何时已处理:

AsyncTask1 a1 = new AsyncTask();
AsyncTask1 a2 = new AsyncTask();

a1.execute();
a2.execute();

a1.get();
a2.get();

Log.d("Example", "a1 and a2 have both finished, you can now proceed");

Another use of AsyncTask is to know when several AsyncTasks have processed:

AsyncTask1 a1 = new AsyncTask();
AsyncTask1 a2 = new AsyncTask();

a1.execute();
a2.execute();

a1.get();
a2.get();

Log.d("Example", "a1 and a2 have both finished, you can now proceed");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文