2013-12-13 33 views
2

如何將我的通知設置爲在點擊時清除自己?如何設置通知以在點擊時自行清除?

我設置autoCancel(true),但它不工作

我的代碼:

Notification n = new Notification.Builder(this) 
       .setContentTitle("Update for you") 
       .setContentText("Please click here to see the update information") 
       .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), 
                  R.drawable.ic_launcher)) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setContentIntent(pList) 
       .setAutoCancel(true) 
       .addAction(R.drawable.ic_read, "Read", pRead) 
       .addAction(R.drawable.ic_list, "All Updates", pList) 
       .build(); 

NotificationManager notificationManager = 
         (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

notificationManager.notify(0, n); 

回答

1

使用FLAG_AUTO_CANCEL

n.flags |= Notification.FLAG_AUTO_CANCEL; 
+0

謝謝您的回答,我要測試,但我不NotificationCompat.Builder類知道爲什麼有時候我的通知顯示,但有時不:( – user2790880

3

Notification.BuildersetAutoCancel()在API 11中引入也許,你的最低版本低於API 11.

setAutoCancel()也可在這是在支持庫添加

Notification n = new NotificationCompat.Builder(this) 
       .setContentTitle("Update for you") 
       .setContentText("Please click here to see the update information") 
       .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), 
                  R.drawable.ic_launcher)) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setContentIntent(pList) 
       .setAutoCancel(true) 
       .addAction(R.drawable.ic_read, "Read", pRead) 
       .addAction(R.drawable.ic_list, "All Updates", pList) 
       .build(); 
+0

謝謝您的回答,我的'機器人:minSdkVersion'是13 – user2790880

+0

順便說一句,什麼是通知和NotificationCompat之間有什麼不同?我應該使用通知NotificationCompat嗎? – user2790880

+0

不,我們仍然使用'Notification'來顯示通知。 'NotificationCompat'是輔助類,用於訪問在向後兼容的API級別4之後引入的'Notification'中的特性。 – Geros

1

SetAutoCancel(真)

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      // .setLargeIcon(image)/*Notification icon image*/ 
      .setSmallIcon(R.drawable.ic_notification) 
      .setContentTitle("Quiz Tap") 
      .setContentText(messageBody) 
      /*.setStyle(new NotificationCompat.BigPictureStyle() 
        .bigPicture(image))*//*Notification with Image*/ 
      **.setAutoCancel(true)** 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 
+0

這是爲我工作 –