我已经看到了这个答案: 如何将OnPostExecute()的结果导入主活动,因为AsyncTask是一个单独的类?
但这不是我要问的,因为我的调用类有多个调用AsyncTask.
我想创建一个通用AsyncTask类,它将接受URL并从给定的URL下载内容并返回下载的内容.
例:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
我有另一个类AsyncTask在不同的场景中调用它
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();
}
}
Run Code Online (Sandbox Code Playgroud)
我知道使用get() …