寻找使用GET()与机器人的的AsyncTask的好榜样 [英] Looking for good example of using get() with an AsyncTask in android

查看:161
本文介绍了寻找使用GET()与机器人的的AsyncTask的好榜样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇中的AsyncTask的 GET(长,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.

<一个href="http://developer.android.com/reference/android/os/AsyncTask.html#get%28long,%20java.util.concurrent.TimeUnit%29">get(long, java.util.concurrent.TimeUnit中)

任何人都可以提供一个例子是使用?

Can anyone provide an example of it's use?

推荐答案

看来好像<一个href="http://developer.android.com/reference/android/os/AsyncTask.html#get%28long,%20java.util.concurrent.TimeUnit%29"><$c$c>AsyncTask.get()阻塞调用者线程,其中<一href="http://developer.android.com/reference/android/os/AsyncTask.html#execute%28Params...%29"><$c$c>AsyncTask.execute()才不是。

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

您可能需要使用 AsyncTask.get()测试情况下,您希望测试特定的Web服务调用,但你并不需要它是异步的,你想控制需要多长时间才能完成。或者,任何时候你想测试的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.

语法是一样的执行:

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

这篇关于寻找使用GET()与机器人的的AsyncTask的好榜样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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