代码与范例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// 添加常驻通知 private void setNotification() { NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.icon, getString(R.string.app_name), System.currentTimeMillis()); Intent intent = new Intent(this, Sample.class); notification.flags = Notification.FLAG_ONGOING_EVENT; // 设置常驻 Flag PendingIntent contextIntent = PendingIntent.getActivity(this, 0, intent, 0); notification.setLatestEventInfo(getApplicationContext(), getString(R.string.app_name), getString(R.string.information), contextIntent); notificationManager.notify(R.string.app_name, notification); } // 取消通知 private void cancelNotification() { NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); notificationManager.cancel(R.string.app_name); } |
说明:通过 setNotification() 可以在通知栏中添加一条常驻通知,通过 cancelNotification() 取消该通知。