显示有关收到的SMS消息的通知



我正在收件箱文件夹中创建一条短信,如下所示:

ContentValues values = new ContentValues();
values.put("address", sender);
values.put("date", System.currentTimeMillis());
values.put("read", 0); // Message not read
values.put("status", 0);
values.put("type", 1);
values.put("seen", 0);
values.put("body", body);
ContentResolver rslv = context.getContentResolver();        
rslv.insert(Uri.parse("content://sms"), values);

这很有效,但不会在顶部栏中触发通常的"收到短信"通知并播放提示音。我该如何编写这样的通知代码(可能会调用某些SMS应用程序的Intent来引发通知)?

通过使用Notification.Builder类来构建通知。一个非常简单的例子是:

Notification noti = new Notification.Builder(mContext)
         .setContentTitle("New SMS from " + sender.toString())
         .setContentText(subject) 
         .setSmallIcon(R.drawable.new_mail)
         .setLargeIcon(aBitmap) 
         .build();   

更多信息可在官方教程中找到。

最新更新