2011-03-17 43 views
10

我已经写了通知功能,并显示在通知栏: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甚至当用户按下“清除” notificatios按钮从通知栏?

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

enter image description here

我曾经使用过此SDK文章了:http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Updating,但未能找到。

+1

你确定保持通知稳定是个好主意吗? – 2011-03-17 11:49:47

+0

@Heiko Rupp抱歉地说是的,但它的实际要求,我必须执行。 – 2011-03-17 12:00:39

+1

读者应该清楚这一点:使用它来宣传你的应用程序将激怒你的用户,它只应用于你需要提供一致的快速访问来控制正在运行的服务。例如:音乐应用程序(仅在播放某些内容时),导航(仅在导航路线时)或GPS记录器(在记录时)。 – 2013-05-28 08:09:46

回答

25

使用FLAG_NO_CLEAR

只需把它放在你的Notification例如,它提供给NotificationManager前:

notificaton.flags |= Notification.FLAG_NO_CLEAR; 

编辑:我只是注意到的是,如果你使用的是Notification.Builder(自蜂窝)和使通知“持续”,它也将免疫“清除所有”。见here。显然,这是为了阻止开发者在非正在进行的通知中使用FLAG_NO_CLEAR,因为这会混淆用户。

+0

@ user634618 +1,正好我在找,我已经实施了国旗和它的工作正常。感谢支持。 – 2011-03-17 13:19:13

12

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