2012-05-23 15 views
9

我有一些創建一些通知的代碼,它非常基礎。Android 4:不能通過刷卡來解除通知

int icon = R.drawable.notification; 
CharSequence tickerText = "Text"; 
long when = System.currentTimeMillis(); 
Notification notification = new Notification(icon, tickerText, when); 

Context context = getApplicationContext(); 
CharSequence contentTitle = "Text"; 
CharSequence contentText = "Text"; 
Intent notificationIntent = new Intent(this, RequestActivity.class); 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
notification.flags |= Notification.DEFAULT_SOUND; 
notification.flags |= Notification.DEFAULT_VIBRATE; 
notification.flags |= Notification.DEFAULT_LIGHTS; 
notification.flags |= Notification.FLAG_AUTO_CANCEL; 

mNotificationManager.notify(notificationID, notification); 

它在2.1中正常工作。 在4.0中,除了滑動解除操作不起作用外,它一切正常。通知稍微向一邊發送,然後粘貼並彈回。 有什麼想法? 謝謝。

+0

嘗試刪除FLAG_AUTO_CANCEL –

+0

@Copa已經嘗試過 – James

+0

MH ...嘗試到第一標誌分配給ntofication.flags,而不是按位或第一個標誌。將notification.flags | = DEFAULT_SOUND更改爲notification.flags = Notification.DEFAULT_SOUND;也許這將有助於 –

回答

12

您無法刷掉您的通知,因爲它處於「正在進行」狀態。

第一溶液:

替換下面的代碼設置標誌:

notification.defaults |= Notification.DEFAULT_SOUND; 
notification.defaults |= Notification.DEFAULT_VIBRATE; 
notification.defaults |= Notification.DEFAULT_LIGHTS; 
notification.flags |= Notification.FLAG_AUTO_CANCEL; 

默認值是默認值截面,標誌截面標誌。

現在它之所以持續下去?

您可能已經知道通知的標誌(和默認值)由bitwise operation設置。意味着每個標誌都有一個2的冪的常量值。添加它們會爲一組標誌產生一個唯一的數字,這使得計算實際設置的標誌真正快速。

Notification.DEFAULT_VIBRATENotification.FLAG_ONGOING_EVENT 具有相同含量的不同的值。

+0

這正是我需要知道的。謝謝! – joshplusa

+0

開始使用setOngoing(false)和notification.flags | = Notification.FLAG_AUTO_CANCEL轉動。 –

0

只需插入這條線時,你的通知...

// Will show lights and make the notification disappear when the presses it 
notification.flags == Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS; 
4

您應該使用setOngoing (boolean ongoing)

設置這是否是一個 「正在進行的」 通知。正在進行的通知 不能被用戶解僱,所以您的應用程序或服務必須在 負責取消它們。它們通常用於指示用戶正在主動參與(例如,播放 音樂)或以某種方式處於待決狀態並因此佔據設備 (例如,文件下載,同步操作,活動網絡連接)的背景任務, 。

您可以使用

.setOngoing(false);