[英]How to remove a notification from the status bar
我正在用Android構建本地通知。 我設法在狀態欄中顯示了本地通知,但是我無法使用setAutoCancel()
或類似的東西將其隱藏。 這是我的代碼:
我的用例是:我顯示了一個本地通知,向用戶表明我正在做一些處理。 完成處理后,我會更改通知中的文字
NotificationManager manager = NotificationManagerCompat.from( this );
NotificationBuilder builder = new NotificationCompat.Builder( getApplicationContext() );
builder.setOngoing( true )
.setSmallIcon( R.drawable.X )
.setContentTitle("Content title")
.setPriority( NotificationCompat.PRIORITY_MAX )
.setProgress( 100, 0, false );
Notification notification = builder.build();
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR | Notification.FLAG_FOREGROUND_SERVICE;
manager.notify( 100, notification );
完成一些處理后,我希望能夠清除通知,因此請執行以下操作:
PendingIntent btPendingIntent = PendingIntent.getActivity( this, 0, new Intent( ), 0 );
NotificationCompat.Builder mb = new NotificationCompat.Builder(this);
mb.setSmallIcon( R.drawable.ic_action_X )
.setLargeIcon( bitmap )
.setContentTitle("Upload complete")
.setAutoCancel( true );
mb.setContentIntent( btPendingIntent );
notificationManager.notify( 100, mb.build() );
通知會根據需要更改文本和圖標,但是當用戶點擊通知時,沒有任何反應-通知不會被取消。
有沒有辦法對通知采取行動,取消通知但不打開任何活動或其他任何動作?
您可以使用NotificationManager.cancel()
並傳入在notify
調用中使用的ID來刪除通知。
notificationManager.cancel(100);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.