2011-03-17 56 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)