将状态栏中的通知更改为toast?

时间:2012-09-28 18:52:40

标签: android notifications broadcastreceiver statusbar toast

我制作短信应用程序。这是课程接收短信:

public class TerimaSMS extends BroadcastReceiver {
    @Override

    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();

        Object messages[] = (Object[]) bundle.get("pdus");
        SmsMessage smsMessage[] = new SmsMessage[messages.length];
        for (int n = 0; n < messages.length; n++) {
        smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
        }

        // show first message
            Toast toast = Toast.makeText(context,
                    "SMS Received : " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
                    toast.show();   

    }
}

如何在状态栏中更改通知的烤面包?谢谢!

2 个答案:

答案 0 :(得分:2)

添加此而不是吐司:

NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Context context = getApplicationContext();          
Intent notificationIntent = new Intent(YourClass.this, YourClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(YourClass.this, 0, notificationIntent, 0);
Notification notification = new Notification(R.drawable.icon, "SMS Received : " + smsMessage[0].getMessageBody(), System.currentTimeMillis());
notification.setLatestEventInfo(context, "title", "content", contentIntent);
mNotificationManager.notify(1, notification);

答案 1 :(得分:0)

这是一个链接,这将有助于您发出通知: - http://mobiforge.com/developing/story/sms-messaging-android