dongyi7041 2013-03-25 08:34
浏览 134
已采纳

在onMessage()上收到推送通知时执行一些代码

This is my code, and I have to execute some http request in an AsyncTask when a push notification is received. As I understood if I need to do something when a push notification arrives, I have to do it within the OnMessage() method. However the app crashes when it receives the notification and does not do what I need it to do: Code:

@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);
}

Thank you very much! (In case you need the whole class just say it. However I can tell you that the PostAsyncTask executes some httpRequest code.)

Logcat:

http://pastebin.com/LggC0uFq

AsyncTask: (It just calls a function that executes some http request)

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
         }
     }
  • 写回答

1条回答 默认 最新

  • drl92080 2013-03-25 09:10
    关注

    I guess, you should not use AsyncTask from the onMessage method of Push Notification. You should call a service which does the task of making http connection. Also remember to use separate thread if you are using Service class as it runs on UI thread only. I hope this helps you out.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度