Android中的AsyncTask的通用类?

我有一个普通的类说,例如类A扩展AsyncTask和所有的方法实现,即onPreExecutedoinbackgroundonPostExecute

现在还有其他的类想要使用Class A对象。

说B类以下面的方式使用A类

 A a = new A(context) a.execute(url) 

然后我在get方法中获取结果。 但是获取方法不是使用AsyncTask的正确方法。 我想在onPostExecute得到结果。 为此,我尝试使用一个布尔参数,只有在onpostexecute才会onpostexecute真。 B类将检查,直到它变为真,当它变成真时,它将取得结果。

但这是以某种方式阻止应用程序。

我已经把asynctask的代码放在下面。

 import java.io.IOException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; import android.app.ProgressDialog; import android.content.Context; import android.os.AsyncTask; public class A extends AsyncTask<String, Void, String> { private Context context = null; private final HttpClient httpClient = new DefaultHttpClient(); private String content = null; //private String error = null; private String finalResult = null; private static boolean isResult = false; private ProgressDialog progressDialog = null; public BabbleVilleSyncTask(Context context) { this.context = context; progressDialog = new ProgressDialog(this.context); } protected void onPreExecute() { progressDialog.setMessage("Please Wait...."); progressDialog.show(); } protected String doInBackground(String... urls) { try { //urls[0] = URLEncoder.encode(urls[0], "UTF-8"); HttpGet httpget = new HttpGet(urls[0]); ResponseHandler<String> responseHandler = new BasicResponseHandler(); content = httpClient.execute(httpget, responseHandler); } /*catch(UnsupportedEncodingException ue) { error = ue.getMessage(); }*/ catch (ClientProtocolException e) { //error = e.getMessage(); cancel(true); } catch (IOException e) { //error = e.getMessage(); cancel(true); } httpClient.getConnectionManager().shutdown(); return content; } protected void onPostExecute(String result) { finalResult = result; progressDialog.dismiss(); System.out.println("on Post execute called"); isResult = true; } public boolean getIsResult() { return isResult; } public void setIsResult(boolean flag) { isResult = flag; } public String getResult() { return finalResult; } } 

有人可以让我知道这个问题可能是什么?

问候

苏尼尔

使用AsyncTask获得结果的干净方法是使用callback接口。

这是这个概念的一个简单的例子:

 interface AsyncTaskCompleteListener<T> { public void onTaskComplete(T result); } 

那么在你的Bclass里:

 class B implements AsyncTaskCompleteListener<String> { public void onTaskComplete(String result) { // do whatever you need } public void launchTask(String url) { A a = new A(context, this); a.execute(url); } } 

您现在应该将以下代码添加到您的A类:

 class A extends AsyncTask<String, Void, String> { private AsyncTaskCompleteListener<String> callback; public A(Context context, AsyncTaskCompleteListener<String> cb) { this.context = context; this.callback = cb; } protected void onPostExecute(String result) { finalResult = result; progressDialog.dismiss(); System.out.println("on Post execute called"); callback.onTaskComplete(result); } } 

这样,你不需要明确地等待你的任务完成,相反,你的主代码(可能是主UI线程)正在等待正常的android事件循环,onTaskComplete方法将被自动调用,允许在那里处理任务结果。

 public abstract class BaseTask<T> extends AsyncTask<Object, Void, T> { public Context context; public ProgressDialog dialog; public Exception exception; protected BaseTask() { } public BaseTask(Context context) { this.context = context; this.dialog = new ProgressDialog(context); } @Override protected void onPreExecute() { this.dialog.setMessage(context.getResources().getString(R.string.loading)); this.dialog.show(); } @Override protected T doInBackground(Object... objects) { try { return doWork(objects); } catch (Exception e) { exception = e; } return null; } @Override protected void onPostExecute(T result) { if (dialog.isShowing()) dialog.dismiss(); if (exception == null) { onResult(result); } else { onError(); } } public abstract T doWork(Object... objects) throws Exception; public abstract void onResult(T result); public abstract void onError(); } 

我会让A类成为父类中embedded的私有类,一旦它完成工作,它应该更新父类的属性,这可能onPostExecute。