![](/img/trans.png)
[英]AsyncTask: how to communicate with another class with onProgressUpdate
[英]Make onPreExecute and onProgressUpdate common for all Asynctask Class
当前,我在android应用程序中工作,因为我有10个单独的Asynctask类,可以进行10个单独的操作,其中,在onPreExecute()和onProgressUpdate()内部调用的用户定义函数对于所有10个Asynctask类都是相同的。 还有其他方法可以简化此过程。 例如,我有一个名为“ ADD”的用户定义函数,到目前为止,我已经在所有10个Asynctask类的onPreExecute()中调用了“ ADD”函数,是否有其他方法可以通过使用interface或其他任何东西
创建一个类,它是扩展AsyncTask的BaseAsyncTask。 并以此编写您的onPreExecute()和onProgressUpdate()的实现。
public abstract class BaseAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
@android.support.annotation.Nullable
private ProgressDialog progressDialog = null;
public Activity activity;
public BaseAsyncTask(Activity activity) {
this.activity = activity;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(activity, R.style.CustomProgressSpinner);
CommonUtilities.showDialog(progressDialog,activity);
}
@Override
protected void onProgressUpdate(Progress... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Result result) {
super.onPostExecute(result);
CommonUtilities.dismissDialog(progressDialog);
}
}
并在所有AsyncTask中扩展该BaseAsyncTask。
public class AttachmentLoadTask extends BaseAsyncTask<DocumentVO, Void, File> {@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected File doInBackground(DocumentVO... documentVOs) {
File file = null;
return file;
}
@Override
protected void onProgressUpdate(Progress... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(File file) {
super.onPostExecute(file);
}}
使用onPreExecute()和onProgressExecute()中的操作创建一个基本的异步任务。 然后创建您的异步任务类(从基本异步任务扩展)。
当然,您可以为所有Asynctask调用创建一个类
只是创建一个类
public class MyConnectionClass extends AsyncTask<Uri, Void, Boolean> {
MyAsyncInterface delegate = null;
HttpURLConnection httpURLConnection;
String output;
public MyConnectionClass(MyAsyncInterface myAsyncInterface) {
delegate = myAsyncInterface;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Boolean aBoolean) {
delegate.processFinish(output);
super.onPostExecute(aBoolean);
}
@Override
protected Boolean doInBackground(Uri... uris) {
try {
URL url = new URL(uris[0].toString());
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.connect();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String line = null;
StringBuilder builder = new StringBuilder();
while ((line = reader.readLine()) != null) {
builder.append(line);
}
output = builder.toString();
return true;
} catch (MalformedURLException e) {
output = e.getMessage();
return false;
} catch (IOException e) {
output = e.getMessage();
return false;
} finally {
httpURLConnection.disconnect();
}
}
}
并声明一个这样的接口
public interface MyAsyncInterface {
void processFinish(String output);
}
然后在您的活动中创建一个Uri并实现MyAsyncInterface,然后在onPostExecute调用processFinish内,并将输出传递给您的调用活动中存在的processFinish(String output)方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.