从GCM Intent Service发送短信并在通知中显示送达报告。

时间:2014-02-11 08:08:49

标签: android android-intent google-cloud-messaging

我正在尝试从GCM Intent Service发送短信。这是我正在使用的代码,但它会中断并给我以下异常,你能告诉我我做错了吗。

 @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
            /*
             * Filter messages based on message type. Since it is likely that GCM will be
             * extended in the future with new message types, just ignore any message types you're
             * not interested in, or that you don't recognize.
             */
            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
               // sendNotification("Send error: " + extras.toString(), null);
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
               // sendNotification("Deleted messages on server: " + extras.toString(), null);
            // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                // This loop represents the service doing some work.
                /*for (int i = 0; i < 5; i++) {
                    Log.i(TAG, "Working... " + (i + 1)
                            + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                    }
                }*/
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                // Post notification of received message.
                Log.i(TAG, "Received: " + extras.toString());
                message =extras.getString("message");
                mNo =extras.getString("mobileNumber");
                isSent =extras.getString("isSent");
                sendMessage(message,mNo);


            }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    // Put the message into a notification and post it.
    // This is just one simple example of what you might choose to do with
    // a GCM message.
    private void sendMessage(String msg, String to) {
        String SENT = "sent";
        String DELIVERED = "delivered";
        Intent sentIntent = new Intent(SENT);
        /*Create Pending Intents*/
        PendingIntent sentPI = PendingIntent.getBroadcast(
          getApplicationContext(), 0, sentIntent,
          PendingIntent.FLAG_UPDATE_CURRENT);

         Intent deliveryIntent = new Intent(DELIVERED);

         PendingIntent deliverPI = PendingIntent.getBroadcast(
          getApplicationContext(), 0, deliveryIntent,
          PendingIntent.FLAG_UPDATE_CURRENT);

         /* Register for SMS send action */
         registerReceiver(new BroadcastReceiver() {

           @Override
          public void onReceive(Context context, Intent intent) {
           String result = "";

            switch (getResultCode()) {

            case Activity.RESULT_OK:
            result = "Transmission successful";
            break;
           case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
            result = "Transmission failed";
            break;
           case SmsManager.RESULT_ERROR_RADIO_OFF:
            result = "Radio off";
            break;
           case SmsManager.RESULT_ERROR_NULL_PDU:
            result = "No PDU defined";
            break;
           case SmsManager.RESULT_ERROR_NO_SERVICE:
            result = "No service";
            break;
           }

            sendNotification("result");
          }

          }, new IntentFilter(SENT));
         /* Register for Delivery event */
         registerReceiver(new BroadcastReceiver() {

           @Override
          public void onReceive(Context context, Intent intent) {
               sendNotification("DELIVERED");
          }

          }, new IntentFilter(DELIVERED));



         contact_no = to;
         SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(contact_no, null,msg, sentPI,deliverPI);

    }

    private void sendNotification(String result) {
        mNotificationManager = (NotificationManager)
                 this.getSystemService(Context.NOTIFICATION_SERVICE);
            Intent notificationIntent = new Intent(this,WelcomeActivity.class);
            notificationIntent.putExtra("SMS",result);
         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
               notificationIntent  , 0);    
         NotificationCompat.Builder mBuilder =
                 new NotificationCompat.Builder(this)
         .setContentTitle("AMD SMS Center")
         .setStyle(new NotificationCompat.BigTextStyle()
         .bigText("SMS" + result));

         mBuilder.setContentIntent(contentIntent);
         mBuilder.setAutoCancel(true);
         mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }

例外情况是:.GcmIntentService $ 1 @ 41ed8458最初在这里注册。你是否错过了对unregisterReceiver()的调用?

1 个答案:

答案 0 :(得分:0)

您无法在BroadcastReceiver中注册IntentServiceIntentService之后onHandleIntent停留,因为您永远不会拨打unregisterReceiver,泄露这个BroadcastReceiver

可能的解决方案;

静态注册您的应用程序以接收您创建的IntentFilter(在清单中)