Android - 在通知栏中保持通知稳定

Par*_*ani 11 android notification-bar android-notification-bar

我已经编写了通知并在通知栏中显示的功能:

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);
    }
Run Code Online (Sandbox Code Playgroud)

它工作正常,但what way we can keep the notification steady at Notification bar即使用户按下通知栏中的"清除"通知按钮?

请查看"Yahoo"应用程序的通知栏.

在此输入图像描述

我已经阅读了这篇SDK文章:http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Updating,但未能找到答案.

use*_*618 26

使用 FLAG_NO_CLEAR

只需将它设置在您的Notification实例上,然后再将其提供给NotificationManager:

notificaton.flags |= Notification.FLAG_NO_CLEAR;
Run Code Online (Sandbox Code Playgroud)

编辑:我刚注意到,如果您正在使用Notification.Builder(自Honeycomb)并使通知"正在进行",它也将免于"全部清除".看到这里.

显然,这应该阻止开发人员使用FLAG_NO_CLEAR非持续通知,因为这可能会使用户感到困惑.


Him*_*ani 14

尝试 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)
Run Code Online (Sandbox Code Playgroud)