onPostExecute中调用的通知返回null

时间:2015-09-24 01:54:56

标签: android

我要在后台服务完成时尝试创建通知消息, 因此,我在异步任务扩展类中的onPostExecute中调用警报消息。 以下是方法。

  

UPDATE    - 为了更好地理解我想要实现的目标以及将来的目的,我编辑了我的工作以包含所需的所有必要信息。感谢

 public class GCMIntentService extends GCMBaseIntentService {

private static final String TAG = "GCMIntentService";
 private Context mContext;
    private int NOTIFICATION_ID = 1;
    private Notification mNotification;
    private NotificationManager mNotificationManager;

public GCMIntentService(Context context) {
    super(SENDER_ID);
    this.mContext = context;

    //Get the notification manager
    mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
}



/**
 * Method called on Receiving a new message
 * */
@Override
protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");

    new ExecuteAsync().execute();
}


private class ExecuteAsync extends AsyncTask<String, String, JSONArray> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected JSONArray doInBackground(String... args) {

        DatabaseHandler db = new DatabaseHandler(getApplicationContext()); 
        UserFunctions userFunction = new UserFunctions();
        Context context= GCMIntentService.this;
        String msg = "";
        JSONArray json = null;

        msg = "syncing was succefuull";
        Log.i("GCM",  msg);

        //This gets all the information unread from the server
        json = userFunction.getAllNotices(context);

        /**
         * This inserts data from the server into the local 
         * database when the query of unread data from the 
         * server was successful.
         */
        if(json!=null){ 

            try{
                for(int i = 0; i < json.length(); i++){

                    //Values from the remote database
                    String value = json.getJSONObject(i).getString("userID");
                    String name = json.getJSONObject(i).getString("fName");
                    System.out.println(value);
                    System.out.println(name);

                    //Insert data into the local database
                    db.insertNotices((value),
                                    (name));

                    //Again update the online database that the message has been received
                   userFunction.syncUpdate(context,value);  
                }

            }
            catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            }
        }
         return json;
     }

   @Override
    protected void onPostExecute(JSONArray json) {
       /**
        * This checks if the on background work was successful
        **/
      if(json != null) {

           System.out.println("json contained a data and it was successful");

           String message = "helllllasdp";
           createNotification("Data download ended abnormally!",message);
      }

     else{
        System.out.println("json was null in the post execute");

      }
    }

   private void createNotification(String contentTitle, String contentText) {

       //Build the notification using Notification.Builder
       Notification.Builder builder = new Notification.Builder(mContext)
       .setSmallIcon(android.R.drawable.stat_sys_download)
       .setAutoCancel(true)
       .setContentTitle(contentTitle)
       .setContentText(contentText);

       //Get current notification
       mNotification = builder.getNotification();

       //Show the notification
       mNotificationManager.notify(NOTIFICATION_ID, mNotification);
   }

}

}

如果有人能帮助我解决这个问题,我将不胜感激。提前谢谢。

2 个答案:

答案 0 :(得分:0)

private class ExecuteAsync extends AsyncTask<Void,Void,Void>
{
    //declare context here
     Context myContext;
     public ExecuteAsync(Context receivedContext)
     {
       this.myContext=receivedContext;
     }

onPostExecute()方法

  protected void onPostExecute(JSONArray json) {

           if(json != null) {
             mNotificationManager = (NotificationManager) myContext.getSystemService(Context.NOTIFICATION_SERVICE);
               //Intent intent=new Intent();
               String message = "helllllasdp";
            Notification.Builder builder = new Notification.Builder(mContext)
           .setSmallIcon(android.R.drawable.stat_sys_download)
           .setAutoCancel(true)
           .setContentTitle("Data download ended abnormally!")
           .setContentText(message);

           //Get current notification
           mNotification = builder.getNotification();

           //Show the notification
           mNotificationManager.notify(NOTIFICATION_ID, mNotification);
           }

           else{
               System.out.println("json was null in the post execute");

           }

}
在您使用onMessage()方法

进行呼叫时,

protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");

    new ExecuteAsync(context).execute();
}

答案 1 :(得分:0)

正如我在评论中提到的,IntentService.onHandleIntent(在这种情况下调用onMessage)在工作线程中运行,因此不需要启动AsyncTask(与启动它们的线程中运行的“普通”服务不同)。

这里发生的是onHandleIntent / onMessage终止并且服务停止意味着上下文变得无效。这很可能是你的Notification.Builder builder = new Notification.Builder(mContext)崩溃的原因。

消除AsyncTask并在onMessage方法中运行包括doInBackground在内的所有内容,你应该很好。