【发布时间】:2013-09-25 14:05:28
【问题描述】:
如果我第一次运行我的应用程序,那么在 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)
【问题讨论】:
-
如果您使用的是
WakefulIntentService,则不需要AsyncTask。IntentService已经为您提供了一个后台线程,WakefulIntentService继承了该线程。您的onHandleIntent()(IntentService) 或doWakefulWork()(WakefulIntentService) 在后台线程上。事实上,fork 另一个后台线程可能会给你带来问题。 -
先生,实际上我通过
AsyncTask与我的server在需要时进行通信并且独立于WakefulIntentService。 -
onPostExecute()中没有WakefulIntentService,因此永远不会运行。onPostExecute()来自AsyncTask。如果您没有使用WakefulIntentService中的AsyncTask,那么WakefulIntentService与您的问题无关。如果您正在使用来自WakefulIntentService的AsyncTask,请不要这样做。 -
好的先生,但请第一次查看我的问题
onpostExecute的AsyncTask正在工作,但在不卸载应用程序的情况下重新运行它没有执行......我已经发布logcat值请检查!