2011-04-17 53 views

回答

82

Notification.FLAG_ONGOING_EVENT指定給您的Notification

示例代碼:

yourNotification.flags = Notification.FLAG_ONGOING_EVENT; 
// Notify... 

如果你不熟悉的Notification API,在Android開發者網站上閱讀Creating Status Bar Notifications

+0

+1了明確的答案! – rekaszeru 2011-04-17 14:29:20

+8

你也應該用'Service.startForeground'啓動正在進行的通知而不使用'NotificationManager.notify()' – Blundell 2012-03-20 08:55:46

+4

不應該是'| =',而不是'='? – Supuhstar 2014-07-25 02:13:26

2
public static final int FLAG_ONGOING_EVENT 

自:API等級1
位被按位或爲,如果這是通知中提到的東西是持續的,就像一個電話,說應設置標誌字段。

public static final int FLAG_FOREGROUND_SERVICE 

自:API級別5 位被按位或爲,如果該通知表示當前運行的服務,應設置標誌字段。

45

它實際上建議使用按位或通知標誌,而不是直接設置標誌。這允許您一次設置多個標誌。

例如:

notification.flags |= Notification.FLAG_ONGOING_EVENT; 
notification.flags |= Notification.FLAG_SHOW_LIGHTS; 

將一次設定兩個標誌,而:

notification.flags = Notification.FLAG_ONGOING_EVENT; 
notification.flags = Notification.FLAG_SHOW_LIGHTS; 

將只設置FLAG_SHOW_LIGHTS標誌。

+0

真的很豐富,謝謝! – KGCybeX 2018-01-12 20:51:13

72

如果您正在使用NotificationCompat.Builder,你可以使用:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
mBuilder.setOngoing(true); //this will make ongoing notification 
+3

upvote因爲現在爲了創建通知我們應該使用Builder。 – 2015-09-14 08:07:57

+1

它需要targetSDK是23 – zys 2016-02-27 08:46:36

+0

@Mahmoud我以後如何解僱它? – mehulmpt 2017-06-22 13:50:51