Android的通用的AsyncTask类 [英] Android Generic AsyncTask Class

查看:182
本文介绍了Android的通用的AsyncTask类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了这样的回答:
<一href=\"http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a\">How获得OnPostExecute(),以主要活动的结果,因为AsyncTask的是一个单独的类?

I have seen this answer: How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

但它不是我问,因为我的调用类有多个呼叫的AsyncTask

But it is not what I am asking since my calling class has multiple calls to the AsyncTask.

我想创建一个通用的的AsyncTask 类将接受URL,并从给定的URL下载内容并返回下载的内容。

I want to create a generic AsyncTask class which will accept the URL and download content from the given URL and return the downloaded content.

例如:

public class MyAsyncTask extends AsyncTask<String, Void, String>{
    ProgressDialog dialog;

    protected void onPreExecute() {
        super.onPreExecute();
        dialog = ProgressDialog.show(context, "Loading", "Please wait...", true);
    }

    protected String doInBackground(String... params) {
        // do download here
    }

    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        dialog.dismiss();
    }
}

和我有另一个类调用该的AsyncTask 在不同的场景

And I have another class which calls this AsyncTask in different scenarios

public class MyClass {

    public method1(String url) {
        String result = new MyAsyncTask().execute(url).get();
    }

    public method2(String url) {
        String result = new MyAsyncTask().execute(url).get();
    }
}

我知道,使用的get()办法将使这一整个过程的同步任务。但是,我想将结果返回给调用的类。而且我也知道,prevent它,我要实现一个接口。但因为我在同一个班有多个调用它是不可能的。因此,谁能给我一个想法解决这个?

I know that using get() method will make this entire process a sync task. But I want to get the result back to calling class. And I also know, to prevent it, I have to implement an interface. But since I have multiple calls in the same class it is not possible. So can anyone give me an idea to solve this?

推荐答案

由于 doInBackground 在后台工作,你无法得到的结果在它的返回值,而从活动通话。

As doInBackground works in background you can not get the result in it's return value while calling from activity.

您需要设置一个回调将被调用的结果。

You need to set a callback which will get called with the result.

有关例如:

interface Callback{
void onResult(String result);
}

请您的活动实现这一点,并在活动

Make your activity implement this and in activity

void onResult(String result){
    //Do something
}

现在改变你的AsyncTask为:

Now change your asyncTask to:

public class MyAsyncTask extends AsyncTask<String, Void, String>{

   ProgressDialog dialog;
   Callback callback;
public MyAsyncTask(Callback callback){
 this.callback=callback;
}
protected void onPreExecute() {
    super.onPreExecute();
    dialog = ProgressDialog.show(context, "Loading", "Please wait...", true);
}

protected String doInBackground(String... params) {
    //do download here
}

protected void onPostExecute(String result) {
    super.onPostExecute(result);
    callback.onResult(result);
    dialog.dismiss();
}
} 

在距离活动开始此任务的时间:

At the time of starting this task from activity:

 new MyAsyncTask(new Callback{
void onResult(String result){ 
 //Do something
}
}).execute(url);

所以,当你得到回应 onResult 将调用

这篇关于Android的通用的AsyncTask类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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