第一次如果我运行我的应用程序,那么在 doinBackground() 完成后,控件会转到 onPostExecute() 。但是如果我在设备上重新运行应用程序而不卸载,则永远不会执行 onPostExecute()。
我有一个活动,我正在调用我的 asynTask 。
代码:
public class AddThing extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
try {
Class.forName("android.os.AsyncTask");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
private View.OnClickListener onSave = new View.OnClickListener() {
startAddThingType2Task();
}
}
private class ThingCallback implements ActionCallback {
Context cntxt;
public ThingCallback(Context context) {
// TODO Auto-generated constructor stub
this.cntxt = context;
}
public void onSuccess(ArrayList<?> objects) {
setProgressBarIndeterminate(false);
Intent intent = new Intent(getApplicationContext(),DocketDetail.class);
startActivityForResult(intent, 1);
}
public void onFailure(Exception exception) {
Toast.makeText(cntxt, "Unable to add docket. Error is: "+ exception.getMessage(), Toast.LENGTH_LONG).show();
}
}
private void startAddThingType2Task() {
final AddThingType2 task = new AddThingType2(AddThing.this,new ThingCallback(this));
runOnUiThread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
task.execute();
}
});
}
我的 asyncTask 类是:
public class AddThingType2Task2 extends AsyncTask<Void, Void, Boolean> {
public ActionCallback callback = null;
private Context context;
private Exception ePriv = null;
private Activity activity;
public AddThingType2(Activity activity,ActionCallback callback, ) {
this.callback = callback;
this.context = context;
this.activity = activity;
}
@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (result == true) {
callback.onSuccess(null);
} else {
callback.onFailure(ePriv);
}
}
@Override
protected Boolean doInBackground(Void... params) {
// TODO Auto-generated method stub
Boolean retval = false;
try {
retval = true;
}
} catch (Exception e) {
Log.e("AddThingType2Task", e.getMessage());
ePriv = e;
}
return retval;
}
}
ActionCallback接口有两个方法 -onSuccess()
和OnFailure()
在其中。
编辑 我也在我的应用程序中使用commonsware 唤醒意图服务。
第一次它像魅力一样工作,但重新运行 onpostExecute() 不起作用......
请帮忙 !!
问候
重新运行后,我在logcat中得到了这个:
09-20 17:37:28.515: W/MessageQueue(9311): Handler{4060df08} sending message to a Handler on a dead thread
09-20 17:37:28.515: W/MessageQueue(9311): java.lang.RuntimeException: Handler{4060df08} sending message to a Handler on a dead thread
09-20 17:37:28.515: W/MessageQueue(9311): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:196)
09-20 17:37:28.515: W/MessageQueue(9311): at android.os.Handler.sendMessageAtTime(Handler.java:457)
09-20 17:37:28.515: W/MessageQueue(9311): at android.os.Handler.sendMessageDelayed(Handler.java:430)
09-20 17:37:28.515: W/MessageQueue(9311): at android.os.Handler.sendMessage(Handler.java:367)
09-20 17:37:28.515: W/MessageQueue(9311): at android.location.LocationManager$ListenerTransport.onStatusChanged(LocationManager.java:206)
09-20 17:37:28.515: W/MessageQueue(9311): at android.location.ILocationListener$Stub.onTransact(ILocationListener.java:75)
09-20 17:37:28.515: W/MessageQueue(9311): at android.os.Binder.execTransact(Binder.java:320)
09-20 17:37:28.515: W/MessageQueue(9311): at dalvik.system.NativeStart.run(Native Method)