我明白这是可能的。至少我看到各种应用程序用于将它们的图标放入通知托盘(例如 Skype)。
我的申请呢?我应该怎么做才能将我的图标或消息放在通知栏中?
我明白这是可能的。至少我看到各种应用程序用于将它们的图标放入通知托盘(例如 Skype)。
我的申请呢?我应该怎么做才能将我的图标或消息放在通知栏中?
您为对象中的通知指定 UI 信息和操作
NotificationCompat.Builder
。要创建通知本身,请调用NotificationCompat.Builder.build()
,它会返回一个Notification
包含您的规范的对象。要发出通知,您可以Notification
通过调用将对象传递给系统NotificationManager.notify()
...
这是将通知消息放入通知托盘的代码。100% 工作。
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this, EndActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
// build notification
// the addAction re-use the same intent to keep the example short
Notification n = new Notification.Builder(this)
.setContentTitle("My message")
.setContentText("Subject")
.setSmallIcon(R.drawable.MyApplogo)
.setContentIntent(pIntent)
.setAutoCancel(true)
.setStyle(new Notification.BigTextStyle().bigText(""+msg.get(3))).build();
// .addAction(R.drawable.line, "", pIntent).build();
n.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, n);
Here is a great example to display the notification.