Android. Сохраняйте уведомление на панели уведомлений.
Я написал функцию для уведомления и отображения на панели уведомлений:
private void showNotification()
{
CharSequence title = "Hello";
CharSequence message = "Notification Demo";
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, "A Notification", System.currentTimeMillis());
Intent notificationIntent = new Intent(this, Main_Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(Main_Activity.this, title, message, pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
}
Работает отлично, но what way we can keep the notification steady at Notification bar
, даже когда пользователь нажимает кнопку "clear" notificatios на панели уведомлений?
Пожалуйста, просмотрите панель уведомлений приложения "Yahoo".
![enter image description here]()
Я просмотрел эту статью в SDK: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Updating, но не смог узнать.
Ответы
Ответ 1
Используйте FLAG_NO_CLEAR
Просто установите его в свой экземпляр Notification
перед его поставкой в NotificationManager
:
notificaton.flags |= Notification.FLAG_NO_CLEAR;
edit: Я только заметил, что если вы используете Notification.Builder
(с Honeycomb) и сделаете уведомление "продолжающимся", оно также станет неуязвимым для "очистить все". См. здесь.
По-видимому, это должно препятствовать разработчикам использовать FLAG_NO_CLEAR
в нерегулярном уведомлении, поскольку это может смутить пользователя.
Ответ 2
Попробуйте setOngoing (true).
notification = new Notification.Builder(getApplicationContext())
.setLargeIcon(
BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher).setTicker(tickerText)
.setWhen(System.currentTimeMillis()).setContentTitle(contentTitle)
.setOngoing(true)
.setContentText(contentText)
.setContentIntent(contentIntent)