Android代码删除通知,Android:从通知中删除通知b
Android:從通知中刪除通知b
我已經創建了一個應用程序,并且我設法在android通知欄中添加通知。 現在我需要示例如何從事件通知欄中刪除該通知?
11個解決方案
197 votes
你可以嘗試這個快速代碼
public static void cancelNotification(Context ctx, int notifyId) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
nMgr.cancel(notifyId);
}
Ankit Patial answered 2019-05-23T02:26:35Z
155 votes
這很簡單。 您必須在NotificationManager上調用cancel或cancelAll。 cancel方法的參數是應該取消的通知的ID。
請參閱API:[http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int)]
RoflcoptrException answered 2019-05-23T02:26:10Z
58 votes
您也可以在通知管理器上調用cancelAll,這樣您甚至不必擔心通知ID。
NotificationManager notifManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.cancelAll();
編輯:我被downvoted所以也許我應該指定這只會從您的應用程序中刪除通知。
Stephane Mathis answered 2019-05-23T02:27:06Z
10 votes
只需像下面的代碼一樣設置setAutoCancel(True):
Intent resultIntent = new Intent(GameLevelsActivity.this, NotificationReceiverActivityAdv.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
GameLevelsActivity.this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
getApplicationContext()).setSmallIcon(R.drawable.icon)
.setContentTitle(adv_title)
.setContentText(adv_desc)
.setContentIntent(resultPendingIntent)
//HERE IS WHAT YOY NEED:
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(547, mBuilder.build());`
farhad.kargaran answered 2019-05-23T02:27:30Z
6 votes
如果要從使用前臺啟動的服務生成通知
startForeground(NOTIFICATION_ID, notificationBuilder.build());
然后發行
notificationManager.cancel(NOTIFICATION_ID);
取消通知& 通知仍會顯示在狀態欄中。 在這種特殊情況下,您將通過兩種方式解決這些問題:
1 GT; 在服務中使用stopForeground(false):
stopForeground( false );
notificationManager.cancel(NOTIFICATION_ID);
2 - ; 使用調用活動銷毀該服務類:
Intent i = new Intent(context, Service.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
if(ServiceCallingActivity.activity != null) {
ServiceCallingActivity.activity.finish();
}
context.stopService(i);
第二種方式更喜歡音樂播放器通知更多因為不僅通知刪除而且還刪除播放器... !!
Dhruv Raval answered 2019-05-23T02:28:30Z
5 votes
這將有助于:
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.cannelAll();
如果您通過調用創建通知
startForeground();
在Service.you可能需要打電話
stopForeground(false);
首先,然后取消通知。
pilotmario answered 2019-05-23T02:29:15Z
1 votes
使用NotificationManager取消通知。 您只需提供通知ID即可
mNotificationManager.cancel(YOUR_NOTIFICATION_ID);
還請查看此鏈接見Developer Link
Muhammad Usman Ghani answered 2019-05-23T02:29:54Z
1 votes
在Android API> = 23上,您可以執行以下操作來刪除一組通知。
for (StatusBarNotification statusBarNotification : mNotificationManager.getActiveNotifications()) {
if (KEY_MESSAGE_GROUP.equals(statusBarNotification.getGroupKey())) {
mNotificationManager.cancel(statusBarNotification.getId());
}
}
Szabolcs Becze answered 2019-05-23T02:30:18Z
1 votes
NotificationManager.cancel(id)是正確的答案。 然而,您可以通過刪除整個通知渠道來刪除Android Oreo及更高版本的通知。 這應刪除已刪除頻道中的所有郵件。
以下是Android文檔中的示例:
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
String id = "my_channel_01";
mNotificationManager.deleteNotificationChannel(id);
gil.fernandes answered 2019-05-23T02:30:49Z
1 votes
請試試這個,
public void removeNotification(Context context, int notificationId) {
NotificationManager nMgr = (NotificationManager) context.getApplicationContext()
.getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancel(notificationId);
}
Akansha patel answered 2019-05-23T02:31:09Z
0 votes
只需撥打ID:
public void delNoti(int id) {((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).cancel(id);}
phnghue answered 2019-05-23T02:31:35Z
總結
以上是生活随笔為你收集整理的Android代码删除通知,Android:从通知中删除通知b的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 零窗口探测怎么抓包_Linux服务器下的
- 下一篇: 【OpenCV 例程200篇】06. 像