正如我所说,这是 AsyncTask 的正确使用吗?
我的意思是,不使用任何参数,在 上传递活动上下文onPreExecute()
,并且没有得到doInBackground()
返回值的结果,而是一旦onPostExecute()
调用了管理器本身。
还有一件事,Asynctask API Reference,它说onPostExecute()
:“如果任务被取消,这个方法将不会被调用。” ,那么如果在执行doInBackground()
方法时发生 SomeException , ¿ 被onPostExecuted()
调用怎么办?
public class MainClass {
...
//Somewhere in the class, the task is called:
new MyAsyncTask.execute();
...
private class MyAsyncTask extends AsyncTask <Void,Void,Void> {
private MyManager manager;
protected void onPreExecute(){
super.onPreExecute();
//We initialice the members here, passing the context like this:
manager = new Manager(MainClass.this);
manager.initializeMembers();
}
protected void doInBackgroud(Void ...params){
try{
//here we use the long operation task that is inside the manager:
manager.startLongTask();
} catch (SomeException e){
doWhatEverItIsWith(e);
}
return null;
}
protected void onPostExecute (Void result){
super.onPostExecute(result);
//Here we get the result from the manager
handleTheResultFromHere(manager.getResult());
}
}
}
谢谢你。