第一次运行我的应用程序后,控件在完成doInBackground()后进入onPostExecute()。但是,如果我在不卸载的情况下重新运行设备上的应用程序,则永远不会执行onPostExecute()。
我有一个活动,从那里我调用我的异步任务。
代码:
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()
。
Edit 我在我的应用程序中也使用了 commonsware wakeful intent service。
第一次它的工作非常好,但在重新运行时,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)
WakefulIntentService
,则无需使用AsyncTask
。IntentService
已经为您提供了一个后台线程,而WakefulIntentService
继承了它。您的onHandleIntent()
(IntentService
)或doWakefulWork()
(WakefulIntentService
)在后台线程上运行。实际上,分叉另一个后台线程可能会导致问题。 - CommonsWareAsyncTask
与我的服务器
进行通信,而且这是独立于WakefulIntentService
的。 - AmreshWakefulIntentService
中没有onPostExecute()
方法,因此它永远不会运行。onPostExecute()
是从AsyncTask
中提取的东西。 如果您没有在WakefulIntentService
中使用AsyncTask
,那么WakefulIntentService
与您的问题无关。 如果您正在从WakefulIntentService
使用AsyncTask
,请不要这样做。 - CommonsWareAsyncTask
的onPostExecute
在第一次运行时是正常的,但在不卸载应用程序的情况下重新运行时它不会被执行...我已经发布了logcat
值,请检查一下! - Amresh