如何在 AsyncTask 中调用一个返回值函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在 AsyncTask 中调用一个返回值函数相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html>
<meta charset="utf-8" />

<script>
function testZ()
var x=9;
var y=1;
var z=x+y;
return z;


function getZ ()
var i = testZ();
alert(i);

</script>

<body>

<div onclick="getZ();" style="color:red;">Click Me 点我..</div>

</body>
</html>

一个函数返回的值是拿来供另一个或其他多个函数使用的,一个函数里面返回的变量值只能在函数内部查看调用,返回值就提供了一个连接多个函数的通道。不然的话你得用全局变量,js里面用全局变量是很糟糕的

js里面时不能像你这样写法一次返回多个值的,只能返回x、y中的一个(其他很多语言这点都比js方便)。

要返回多个的话,你可以把x、y放到数值之类的玩意里面,然后再把整个数组返回出去。接收处理数据的另一个函数再把数组里面的x、y分别取出来使用。

有几种方法返回多个值,你百度下:js返回多个值
参考技术A 解决方法 1:
为我自己我做这一个回调函数,调用后 onPostExecute。
public AsyncUnzip(Activity ctx, Observer callback)
this.ctx = ctx;
this.callback = callback;



@Override
protected void onPreExecute()
super.onPreExecute();
dia = new ProgressDialog(ctx);
dia.setTitle("Bitte warten");
dia.setMessage("Geodatenpaket wird entpackt...");
dia.setCancelable(false);
dia.show();



@Override
public void onPostExecute( Boolean result )
super.onPostExecute(result);
dia.dismiss();
callback.update(null, returnFolder);
System.out.println("Unzipped to: " + returnFolder.getName() );


在这种情况下您的异步任务的电话会这样看:
AsyncUnzip unzipThread = new AsyncUnzip(ImportActivity.this, new Observer()
@Override
public void update( Observable observable, Object data ) //your code invoked after Async Task
);
unzipThread.execute(selectedFile); //Start Unzip in external Thread.

注: 这是一个快速和 diry 的解决方案有匿名观察员执行和没有可测的。
最新最早最热
0条评论

AsyncTask:doInBackground() 的返回值去哪了?

【中文标题】AsyncTask:doInBackground() 的返回值去哪了?【英文标题】:AsyncTask: where does the return value of doInBackground() go? 【发布时间】:2011-05-28 05:25:43 【问题描述】:

调用AsyncTask&lt;Integer,Integer,Boolean&gt;时,返回值在哪里:

protected Boolean doInBackground(Integer... params)?

通常我们以 new AsyncTaskClassName().execute(param1,param2......); 启动 AsyncTask,但它似乎没有返回值。

doInBackground()的返回值在哪里可以找到?

【问题讨论】:

【参考方案1】:

然后该值在 onPostExecute 中可用,您可能需要覆盖它以使用结果。

这是来自 Google 文档的示例代码 sn-p:

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

【讨论】:

为了使这个除了记录或显示对话框之外的任何实际用途,您可以在您的 UI(或其他)类中嵌套您扩展 AsyncTask 的类,然后从该类调用一个方法使用onPostExecute 方法中的返回值。【参考方案2】:

您可以通过调用AsyncTask 类的get() 方法来检索受保护的布尔值doInBackground() 的返回值:

AsyncTaskClassName task = new AsyncTaskClassName();
bool result = task.execute(param1,param2......).get(); 

但要注意 UI 的响应能力,因为 get() 会等待计算完成并会阻塞 UI 线程。 如果您使用的是内部类,最好在 onPostExecute(Boolean result) 方法中完成这项工作。

如果您只是想更新 UI,AsyncTask 为您提供两种可能性:

要与doInBackground() 中执行的任务并行更新 UI(例如更新ProgressBar),您必须在doInBackground() 方法中调用publishProgress()。然后你必须在 onProgressUpdate() 方法中更新 UI。

要在任务完成后更新 UI,您必须在 onPostExecute() 方法中进行。
/** This method runs on a background thread (not on the UI thread) */
@Override
protected String doInBackground(String... params) 
    for (int progressValue = 0; progressValue  < 100; progressValue++) 
        publishProgress(progressValue);
    


/** This method runs on the UI thread */
@Override
protected void onProgressUpdate(Integer... progressValue) 
    // TODO Update your ProgressBar here


/**
 * Called after doInBackground() method
 * This method runs on the UI thread
 */
@Override
protected void onPostExecute(Boolean result) 
   // TODO Update the UI thread with the final result

这样您就不必关心响应问题。

【讨论】:

这不是正确的方法 .get() 将冻结 UI 你能建议任何其他选项我想使用异步任务的返回结果然后想执行其他网络服务请帮助我 如果你在第五行之后阅读我的帖子,你会看到我建议使用OnPostExecute() 而不是get() 以避免阻塞UI线程。 是的,但我想使用返回值,并且在返回值的基础上我想调用多个 url,那么我能做些什么,请帮助我 然后你必须定义一个返回你想要的类型的AsyncTask。可能是字符串而不是布尔值:private class MyAsyncTask extends AsyncTask&lt;String, int[], String&gt;(最后一个参数定义结果类型)。然后你可以从 onPostExecute(String result) 推断和调用你的 url。 非常有帮助。谢谢!

以上是关于如何在 AsyncTask 中调用一个返回值函数的主要内容,如果未能解决你的问题,请参考以下文章

android AsyncTask 怎么返回值给UI线程

无法从 AsyncTask 返回值;使用接口和委托;

有时在AsyncTask中返回值为null

使用 AsyncTask 并将值返回给 UI 主线程

android AsyncTask 怎么返回值给UI线程

android AsyncTask 怎么返回值给UI线程