Android中传入短信的状态栏通知?

时间:2015-03-09 07:58:08

标签: android notifications sms statusbar

我是Android编程的初学者。我正在尝试在Android设备中实现传入SMS的通知。但是,我能够为此创建一个toast但无法实现状态栏通知。 你能帮帮我吗?

现在我编写了以下代码:

 @Override
        public void onReceive(Context arg0, Intent arg1) 
        {

    //      Log.i(TAG,"OnReceive ++      ");
            Bundle bndl = arg1.getExtras();
            SmsMessage[] msg = null;

            if (null != bndl)
            {
                //---retrieve the SMS message received---
                Object[] pdus = (Object[]) bndl.get("pdus");
                msg = new SmsMessage[pdus.length];         
                for (int i=0; i<msg.length; i++)
                {
                    msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]);             
                    str += "SMS From " + msg[i].getOriginatingAddress();                   
                    str += " :\r\n";
                    str += msg[i].getMessageBody().toString();
                    str += "\n";
                    context = arg0;
                }

                //---display incoming SMS as a Android Toast---
                Toast.makeText(arg0, str, Toast.LENGTH_SHORT).show();
                @SuppressWarnings("unused")
                class MynotificationMain extends Activity 
                {
                    int mNotificationId = 001;

                    @Override
                    protected void onCreate(Bundle savedInstanceState) 
                    {
                        super.onCreate(savedInstanceState);
                        //setContentView(R.layout.activity_mynotification_main);
                        NotificationCompat.Builder  mBuilder = new   NotificationCompat.Builder(context);
                          mBuilder.setContentTitle("New Message");
                          mBuilder.setContentText(str);
                          mBuilder.setTicker("New Message Alert!");
                          mBuilder.setSmallIcon(R.drawable.notification);

                        Intent resultIntent  = new Intent(this, MainActivity.class );
                        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

                        mBuilder.setContentIntent(resultPendingIntent);

                        NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                        mNotifyMgr.notify(mNotificationId, mBuilder.build());

                        MapsFragment obj = new MapsFragment();
                        obj.onLocationChanged(null);
                    }
                }
            }

        }
    }

1 个答案:

答案 0 :(得分:1)

为什么要在方法中进行活动?它应该是

@Override
public void onReceive(Context arg0, Intent arg1) 
{
 Bundle bndl = arg1.getExtras();
 SmsMessage[] msg = null;
 if (null != bndl)
 {
   //---retrieve the SMS message received---
   Object[] pdus = (Object[]) bndl.get("pdus");
   msg = new SmsMessage[pdus.length];         
   for (int i=0; i<msg.length; i++)
   {
     msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]);             
     str += "SMS From " + msg[i].getOriginatingAddress();                   
     str += " :\r\n";
     str += msg[i].getMessageBody().toString();
     str += "\n";
   }

   //---display incoming SMS as a Android Toast---
   Toast.makeText(arg0, str, Toast.LENGTH_SHORT).show();
   //---Make a notification          
   NotificationCompat.Builder  mBuilder = new   NotificationCompat.Builder(context);
   mBuilder.setContentTitle("New Message");
   mBuilder.setContentText(str);
   mBuilder.setTicker("New Message Alert!");
   mBuilder.setSmallIcon(R.drawable.notification);

   Intent resultIntent  = new Intent(this, MainActivity.class );
   PendingIntent resultPendingIntent = PendingIntent.getActivity(arg0, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

   mBuilder.setContentIntent(resultPendingIntent);

   NotificationManager mNotifyMgr = (NotificationManager) arg0.getSystemService(arg0.NOTIFICATION_SERVICE);
   mNotifyMgr.notify(mNotificationId, mBuilder.build());

 }
}
相关问题