java Android,我可以将 AsyncTask 放在单独的类中并进行回调吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10768449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:28:12  来源:igfitidea点击:

Android, can I put AsyncTask in a separate class and have a callback?

javaandroidandroid-asynctask

提问by Roger Travis

I'm just learning about AsyncTask and want to use it as a separate class, rather then a subclass.

我只是在学习 AsyncTask 并希望将它用作单独的类,而不是子类。

For example,

例如,

class inetloader extends AsyncTask<String, Void, String> {


    @Override
    protected String doInBackground(String... urls) {

        String response = "";

            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(urls[0]);
            try {
                HttpResponse execute = client.execute(httpGet);
                InputStream content = execute.getEntity().getContent();

                BufferedReader buffer = new BufferedReader(
                        new InputStreamReader(content));
                String s = "";
                while ((s = buffer.readLine()) != null) {
                    response += s;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        return response;
    }


    @Override
    protected void onPostExecute(String result) {
        Log.e("xx",result);
        // how do I pass this result back to the thread, that created me?
    }


}

and the main(ui) thread:

和主(ui)线程:

inetloader il = new inetloader();
il.execute("http://www.google.com");
//il.onResult()
//{
    ///do something...
//}

Thanks!

谢谢!

回答by Xi Zhang

Use a interface. Something like:

使用接口。就像是:

interface CallBackListener{
  public void callback();
}

Then do this in your UI thread:

然后在您的 UI 线程中执行此操作:

inetloader il = new inetloader();
li.setListener(this);
il.execute("http://www.google.com");

In inetloader, add:

在 inetloader 中,添加:

   CallBackListener mListener;

   public void setListener(CallBackListener listener){
     mListener = listener;
   }

then In postExecute(), do:

然后在 postExecute() 中,执行:

   mListener.callback();

回答by Dheeresh Singh

you can pass the activity instance to constructor and call activity function from there...
Like use interface :

您可以将活动实例传递给构造函数并从那里调用活动函数......
就像使用接口一样:

public interface ResultUpdatable {

   public void setResult(Object obj);
   }

Implement this in the Activity and pass in the constructor of Async task and update the result from onPostExecuteusing setResultfunction.

在 Activity 中实现它并传入异步任务的构造函数并更新onPostExecute使用setResult函数的结果。

回答by Samir Mangroliya

inetloader il = new inetloader();
il.execute("http://www.google.com");

String result = il.get();//put it in try-catch
                ^^^^^^^^ 

here you get result which is in onPostExecute(String result)

here you get result which is in onPostExecute(String result)