2011-11-09 41 views
0

我有下面的代碼來創建一個ONGOING_EVENT隱藏正在進行的通知

Notification notification = new Notification(R.drawable.icon, "someText" , System.currentTimeMillis()); 
notification.flags |= Notification.FLAG_ONGOING_EVENT; 
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; 

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Home.class), 0); 
notification.setLatestEventInfo(this, "someTitle", "someText", contentIntent); 

notificationManager.notify(SOME_ID, notification); 

現在,我要殺死它以後:

notificationManager.cancel(SOME_ID); 

但是,通知保持。這可能是什麼原因?

+1

http://stackoverflow.com/questions/5232650/android-notification-delete這可能有助於您的博愛。 – drooooooid

回答

3

問題是notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;。我誤解了這個標誌的文檔,隨後無法刪除通知。該標誌通常通過以startForeground(int id, Notification notification)開始的服務來設置,並且防止在通過stopForeground(boolean removeNotification)停止該服務之前移除該通知。

我現在使用startForeground/stopForeground方法,通知顯示/隱藏完美。

0

我還沒有實施此標誌,但檢查通知標誌FLAG_AUTO_CANCEL。在創建通知之前添加此項。

+0

這會在用戶點擊通知時取消通知,而不是我想實現的通知,因爲通知詳細介紹了上傳。 – Femaref