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

时间:2011-03-17 11:40:49

标签: 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);
    }

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

请查看“Yahoo”应用程序的通知栏。

enter image description here

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

2 个答案:

答案 0 :(得分:25)

使用FLAG_NO_CLEAR

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

notificaton.flags |= Notification.FLAG_NO_CLEAR;

编辑:我只是注意到,如果您使用Notification.Builder(自Honeycomb)并使通知“正在进行”,它也将免于“全部清除”。请参阅here

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

答案 1 :(得分:13)

尝试 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)