寻找在 android 中使用 get() 和 AsyncTask 的好例子 [英] Looking for good example of using get() with an AsyncTask in android

查看:23
本文介绍了寻找在 android 中使用 get() 和 AsyncTask 的好例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 AsyncTask 中的 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(长,java.util.concurrent.TimeUnit)

谁能提供一个使用的例子?

Can anyone provide an example of it's use?

推荐答案

看起来好像 AsyncTask.get() 阻塞调用者线程,其中 AsyncTask.execute() 没有.

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

您可能希望将 AsyncTask.get() 用于要测试特定 Web 服务调用的测试用例,但您不需要它是异步的,并且您想控制如何需要很长时间才能完成.或者任何时候您想在测试套件中针对您的网络服务进行测试.

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.

语法与execute相同:

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);

这篇关于寻找在 android 中使用 get() 和 AsyncTask 的好例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆