4

第一次如果我运行我的应用程序,那么在 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)
4

3 回答 3

0
public class AddThing extends Activity {
   private ThingCallback callback;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ThingCallback = new ThingCallback(this);
            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) {
           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, callback);
   task.execute();
}
于 2013-09-20T10:21:43.310 回答
0

您是否使用任何其他线程,然后是 AsyncTask?如果没有,则无需使用 runOnUiThread。删除它并且不要使 AsyncTask 引用最终。AsyncTask 永远不能执行两次,这就是为什么每次再次使用它时都必须实例化它的原因。final 关键字将阻止新的实例化并使引用不可变。

private void startAddThingType2Task() {
   AddThingType2 task = new AddThingType2(AddThing.this,new ThingCallback(this));
   task.execute();
}

如果您需要 runOnUiThread,请考虑将 AsyncTask 实例声明为字段。这样你就不必让它最终在 runOnUiThread 中使用它。

编辑:

你会有这样的事情:

public class AddThing extends Activity {
        // Your field
        AddThingType2 task;

        @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 void startAddThingType2Task() {
            // no final needed and you can instantiate it multiple times
            task = new AddThingType2(AddThing.this, callback);
            task.execute();
        }
}
于 2013-09-20T10:31:58.387 回答
0

把这个类加载到应用程序的 onCreate

public class MyApplication extends android.app.Application{

   public void onCreate(){
     try {
       Class.forName("android.os.AsyncTask");
     } catch (ClassNotFoundException e) {
       e.printStackTrace();
     }

   }
}
于 2013-11-19T18:49:03.250 回答