这是不好的做法吗?
import android.app.Activity;
import android.os.AsyncTask;
public abstract class Asynch extends Activity {
public abstract void doInBackgroundAbstract();
public abstract void onPostExecuteAbstract();
protected class AsynchtaskSubClass extends AsyncTask {
@Override
protected Object doInBackground(Object... arg0) {
doInBackgroundAbstract();
return null;
}
protected void onPostExecute(Object result){
onPostExecuteAbstract();
}
}
}
public class SomeActivity extends Asynch {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_classes);
new AsynchtaskSubClass().execute();
}
private Document doc;
@Override
public void doInBackgroundAbstract() {
// NETWORK OPERATION GOES HERE!
try {
doc = Jsoup.connect("mySite").get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onPostExecuteAbstract() {
// Update UI Thread Here
TextView myTextView = new TextView(SomeActivity.this);
myTextView.setText(doc.select("a[href]").attr("abs:href").toString());
setContentView(myTextView);
}
这对我来说看起来更干净。我不必继续创建内部异步任务或创建单独的类来运行异步任务。如果我想拥有一个在许多活动中重用的异步任务,那么我只需扩展异步并覆盖抽象方法并将其保留为单独的类。他们这样做有什么问题吗?我看到的唯一问题是,如果我需要在一个活动中使用两个或多个异步任务。