1

我在使用 AsyncTask 的 OnPOstExecute 时遇到问题,它运行良好,但如果应用程序停止运行,则会生成一个通知,该通知会打开运行 AsyncTask 的 Activity,但永远不会调用 OnPOstExecute。

我的疑问是:如果应用程序已关闭,当我尝试从通知中打开活动时,为什么 OnPOstExecute 不起作用。

代码:

      protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
        ...
        ...
               try{
                    loadListTask = new LoadListTask();
                    loadListTask.execute();
                }catch(Exception e){
                    Log.d(TAG,"ERRO: "+e.getMessage());
                 }
      }


public void loadListView() {
        messagesDb = useful.buscarMessagesDb(this);
        ArrayList<Message> messageListViews = new ArrayList<Message>();
        int messagesDbSize = messagesDb.size();
        for (int i = 0; i < messagesDbSize; i++) {
            if (((messagesDb.get(i).idFrom == myId) && (messagesDb.get(i).idTo == idContact)) || 
                    ((messagesDb.get(i).idFrom == idContact) && (messagesDb.get(i).idTo == myId))) {
                Message message = new Message(messagesDb.get(i).idFrom, messagesDb.get(i).idTo, 
                        messagesDb.get(i).dt, messagesDb.get(i).mensagem, messagesDb.get(i).confirm,
                        messagesDb.get(i).idWeb);           
                messageListViews.add(message);
            } else {
                //Log.d(TAG, "Essa mensagem não é pra esse contato!!!");
            }
        }
            listChat.setAdapter(null);
            pref.edit().putBoolean("isUpdate", false).commit();
            AdapterMessageListView adapterMessageListView = new AdapterMessageListView(myId, idContact, 
                    getApplicationContext(), messageListViews); 
            listChat.setAdapter(adapterMessageListView);
            listChat.setCacheColorHint(Color.TRANSPARENT);
            listChat.setSelection(listChat.getCount());
    }

private class LoadListTask extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {
            try {
                Log.d(TAG,"doInBackground");
                Thread.sleep(100);
            } catch (InterruptedException e) {

            }
            return "list";
        }
            @Override
            public void onPreExecute() {
                Log.d(TAG,"PREEXECUTE");

            }

            @Override
            protected void onPostExecute(String result) {

                Log.d(TAG,"PostExecute");
                loadListView();
                loadListTask = new LoadListTask();
                loadListTask.execute();
                super.onPostExecute(result);
            }
        }

信息:

02-25 14:05:42.396: D/Chat(20663): doInBackground
02-25 14:05:42.396: W/MessageQueue(20663): Handler{40532ad8} sending message to a Handler on a dead thread
02-25 14:05:42.396: W/MessageQueue(20663): java.lang.RuntimeException: Handler{40532ad8} sending message to a Handler on a dead thread
4

2 回答 2

0

根据文档 http://developer.android.com/reference/android/os/AsyncTask.html

onPostExecute(Result),在后台计算完成后在 UI 线程上调用。>后台计算的结果作为参数传递给这一步。

一旦你的应用程序关闭,就不再有 UI 线程了。这就是日志显示“在死线程上”语句的原因。

于 2013-02-25T21:47:40.403 回答
0

这个解决方案对我有用:

// workaround for http://code.google.com/p/android/issues/detail?id=20915
try {
        Class.forName("android.os.AsyncTask");
} catch (ClassNotFoundException e) {
}

http://oldcodemonkey.blogspot.com.br/2012/06/android-sending-message-to-handler-on.html https://code.google.com/p/android/issues/detail?id=20915

于 2013-09-16T18:00:59.877 回答