无法保存短信或彩信

时间:2012-02-24 16:15:51

标签: android

嘿伙计们查看这段代码......

一切正常但是当我收到短信的通知时,我下拉抽屉并点击它......没有任何反应...... 我该怎么做才能存储短信并显示它,因为Android股票消息应用程序显示它们...

请帮助

      public void onReceive(Context context, Intent intent)
{
          /* As we want to display a Notification, we the NotificationManager */
          NotificationManager nm =
               (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
          if (intent.getAction().equals(ACTION)) {
               // if(message starts with SMStretcher recognize BYTE)
               StringBuilder sb = new StringBuilder();
               String from = new String();
               String body = new String();

               /* The SMS-Messages are 'hiding' within the extras of the Intent. */
               Bundle bundle = intent.getExtras();
               if (bundle != null) {
                    /* Get all messages contained in the Intent*/
                   Object[] pduObj = (Object[]) bundle.get("pdus");
                   SmsMessage[] messages = new SmsMessage[pduObj.length];

                   for(int i=0;i<pduObj.length;i++)
                   {
                           messages[i]=SmsMessage.createFromPdu((byte[])pduObj[i]);

                   }
                    /* Feed the StringBuilder with all Messages found. */

                    for (SmsMessage currentMessage : messages){
                        from = currentMessage.getDisplayOriginatingAddress();
                        body = currentMessage.getDisplayMessageBody();

                         /* Sender-Number */
                         sb.append(from);

                         /* Actual Message-Content */
                         sb.append(body);
                    }
               }
               /* Logger Debug-Output */
               Log.i(LOG_TAG, "[SMSApp] onReceive: " + sb);
               /* Show the Notification containing the Message. */

               int icon=R.drawable.messageicon;

               CharSequence tickerText =  from + ": " + body;
               long when = System.currentTimeMillis();

               Notification notification = new Notification(icon, tickerText, when);
               CharSequence contentTitle = from;
               CharSequence contentText = sb.toString();
               Intent notificationIntent = new Intent();
               PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

               notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
             //  notification.vibrate = new long[] { 100, 250, 100, 500};
               notification.flags |= Notification.FLAG_AUTO_CANCEL;

               nm.notify(1, notification);

               this.abortBroadcast();
               /* Start the Main-Activity 
               Intent i = new Intent(context, SMS.class);
               i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               context.startActivity(i);*/
          }
     }
}

1 个答案:

答案 0 :(得分:0)

这是有道理的,你的代码不应该按原样启动任何活动。

您提供给通知管理器的意图(notificationIntent)是在初始化为活动时启动活动的意图,例如

notificationIntent = new Intent(context, SMS.class);