目前,我在android应用程序中工作,因为我有10个单独的Asynctask类用于10个单独的操作,其中,在onPreExecute()和onProgressUpdate()内部调用的用户定义函数对于所有10个Asynctask类都是相同的。有没有其他方法来简化这一点。例如,我有一个名为" ADD"的用户定义函数。 ,到目前为止,我已经打电话给" ADD"函数在所有10个Asynctask类的onPreExecute()中,有没有其他方法来简化这个,通过使用接口或任何其他,
答案 0 :(得分:3)
创建一个BaseAsyncTask类,它扩展了AsyncTask。 并在此处编写onPreExecute()和onProgressUpdate()int的实现。
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);
}}
答案 1 :(得分:0)
使用onPreExecute()和onProgressExecute()中的操作创建基本异步任务。然后创建异步任务类(从基本异步任务扩展)。
答案 2 :(得分:0)
当然,您可以为所有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,然后在onPostExecute调用processFinish中实现MyAsyncInterface,并将输出传递给你的调用活动中出现的processFinish(String output)方法