1

这是我的代码,当收到推送通知时,我必须在AsyncTask中执行一些http请求。 据我了解,如果我需要在推送通知到达时执行某些操作,我必须在OnMessage()方法中执行此操作。 然而,应用程序崩溃时收到通知,并没有做什么,我需要它做的事: 代码:在onMessage()上收到推送通知时执行一些代码

@Override 
protected void onMessage(Context context, Intent intent) { 
    Log.i(TAG, "Received message"); 
    String message = intent.getExtras().getString("price"); 
    //sends info to the server 

    new PostAsyncTask().execute(); 

    displayMessage(context, message); 
    // notifies user 
    generateNotification(context, message); 
} 

非常感谢您! (如果您需要全班只说这不过我可以告诉你的是,PostAsyncTask执行一些HttpRequest的代码。)

的logcat:

http://pastebin.com/LggC0uFq

的AsyncTask: (它只是调用执行一些HTTP请求的功能)

class PostAsyncTask extends AsyncTask<String, Integer, Boolean> { 
    protected Boolean doInBackground(String... params) { 
     ReNewCoordinates(); 
     postData(lat, lon); 
      return true; 

    } 

    protected void onPreExecute() { 
     // show progress dialog 
    } 


     protected void onPostExecute(Long result) { 
      // hide progress dialog 
     } 
    } 
+0

你能告诉我们你的logcat吗? – 2013-03-25 08:46:56

+0

而不是使用AsyncTask使用线程,我正在使用相同的地方,我在线程下载图像时收到推送通知。 – andrewww 2013-03-25 08:51:59

+0

我不认为这是问题。即使我不使用asyncTask,只是调用一些执行请求的方法它会压碎。 – NirPes 2013-03-25 08:55:22

回答

0

我猜,你不应该从推送通知的onMessage方法使用的AsyncTask。您应该调用一个服务来完成建立http连接的任务。另外请记住,如果您使用Service类时使用单独的线程,因为它仅在UI线程上运行。我希望这能够帮到你。