2013-05-16 124 views
0

我收到通知,我想關閉按鈕單擊。我使用SetAutoCancel,但它不起作用。任何人都知道如何實現它?使用此按鈕remoteViews.setOnClickPendingIntent(R.id.notificationbutton, pIntent);Android通知SetAutoCancel無法正常工作

代碼

RemoteViews remoteViews = new RemoteViews(getPackageName(), 
       R.layout.customnotification); 

     String strtitle = getString(R.string.customnotificationtitle); 

     Intent intent = new Intent(this, NotificationView.class); 

     intent.putExtra("text", strtext); 
     PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 
       PendingIntent.FLAG_UPDATE_CURRENT); 

     remoteViews.setOnClickPendingIntent(R.id.notificationbutton, pIntent); 

     NotificationCompat.Builder builder = new NotificationCompat.Builder(
       this).setSmallIcon(R.drawable.logosmall) 
       .setTicker(getString(R.string.customnotificationticker)) 
       .setAutoCancel(true).setContentIntent(pIntent) 
       .setContent(remoteViews); 

     remoteViews.setImageViewResource(R.id.imagenotileft, 
       R.drawable.ic_launcher); 
     remoteViews.setTextViewText(R.id.title, 
       getString(R.string.customnotificationtitle)); 
     remoteViews.setTextViewText(R.id.text, 
       getString(R.string.customnotificationtext)); 

     NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     notificationmanager.notify(0, builder.build()); 

回答

3

autoCancel工作只有默認的通知點擊調用,如果你正在使用的按鈕,你必須清除通知你的自我

題外話,爲什麼使用自定義按鈕時,有builder.addAction?

+0

是的,我知道addAction。我想嘗試一個自定義按鈕。你如何清除通知? –

+0

您的NotificationManager實例在您的案例中調用clear(int id)id爲0 – mstrengis

+0

對於NotificationManager類型,方法clear(int)未定義。不能這樣做,我猜 –

相關問題