android销毁通知栏方法,如何在Android中清除通知

如何在Android中清除通知

是否可以通过编程方式清除通知?

我尝试使用NotificationManager,但它无法正常工作。还有其他办法吗?

rahul asked 2019-08-12T05:48:42Z

12个解决方案

215 votes

使用以下代码取消通知:

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.cancel(NOTIFICATION_ID);

在此代码中,始终存在用于通知的相同ID。 如果您有不同的通知需要取消,则必须保存用于创建通知的ID。

Janusz answered 2019-08-12T05:49:02Z

41 votes

来自:[http://developer.android.com/guide/topics/ui/notifiers/notifications.html]

要在用户从“通知”窗口中选择状态栏通知时清除状态栏通知,请添加" FLAG_AUTO_CANCEL" 标记到Notification对象。 您也可以使用cancel(int)手动清除它,向其传递通知ID,或使用cancelAll()清除所有通知。

但是Donal是对的,你只能清除你创建的通知。

Chuck C. answered 2019-08-12T05:49:42Z

32 votes

由于没有人发布代码答案:

notification.flags = Notification.FLAG_AUTO_CANCEL;

..如果你已经有了旗帜,你可以像这样:或者FLAG_AUTO_CANCEL:

notification.flags = Notification.FLAG_INSISTENT | Notification.FLAG_AUTO_CANCEL;

NPike answered 2019-08-12T05:50:14Z

14 votes

请尝试在NotificationManager中提供的默认方法。

NotificationManager.cancelAll()删除所有通知。NotificationManager.cancel(notificationId)删除特定通知。

Rohit Suthar answered 2019-08-12T05:50:46Z

8 votes

从API级别18(Jellybean MR2)开始,您可以通过NotificationListenerService取消您自己的通知。

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)

public class MyNotificationListenerService extends NotificationListenerService {...}

...

private void clearNotificationExample(StatusBarNotification sbn) {

myNotificationListenerService.cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());

}

Jim Vitek answered 2019-08-12T05:51:13Z

3 votes

如果您正在使用getNotification()(android.support.v4的一部分),那么只需调用其对象的方法setAutoCancel

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

builder.setAutoCancel(true);

有些人报告getNotification()没有为他们工作,所以你也可以尝试这种方式

builder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;

请注意,方法getNotification()已被弃用!!!

sandalone answered 2019-08-12T05:51:53Z

3 votes

Notification mNotification = new Notification.Builder(this)

.setContentTitle("A message from: " + fromUser)

.setContentText(msg)

.setAutoCancel(true)

.setSmallIcon(R.drawable.app_icon)

.setContentIntent(pIntent)

.build();

.setAutoCancel(真)

单击通知时,打开相应的活动并从通知栏中删除通知

Neeraj Singh answered 2019-08-12T05:52:26Z

1 votes

// Get a notification builder that's compatible with platform versions

// >= 4

NotificationCompat.Builder builder = new NotificationCompat.Builder(

this);

builder.setSound(soundUri);

builder.setAutoCancel(true);

如果您使用通知构建器,这是有效的...

user2962552 answered 2019-08-12T05:52:51Z

1 votes

String ns = Context.NOTIFICATION_SERVICE;

NotificationManager Nmang = (NotificationManager) getApplicationContext()

.getSystemService(ns);

Nmang .cancel(getIntent().getExtras().getInt("notificationID"));

如需更多参考,请点击此处[http://androiddhina.blogspot.in/2015/01/how-to-clear-notification-in-android.html]

Dhina k answered 2019-08-12T05:53:16Z

1 votes

实际上,在启动API Level 18之前,您可以取消其他应用程序发布的Notifications,而不是您自己使用NotificationListenerService,但该方法将不再适用于Lollipop,这是删除Lillipop API通知的方法。

if (Build.VERSION.SDK_INT < 21) {

cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());

}

else {

cancelNotification(sbn.getKey());

}

Alejandro Casanova answered 2019-08-12T05:53:43Z

0 votes

如果要从使用前台启动的服务生成通知

startForeground(NOTIFICATION_ID, notificationBuilder.build());

然后发行

notificationManager.cancel(NOTIFICATION_ID);

最终取消通知,通知仍会显示在状态栏中。 在这种特殊情况下,您需要发布

stopForeground( true );

从服务内部将其重新置于后台模式并同时取消通知。 或者,您可以将其推送到后台而不取消通知,然后取消通知。

stopForeground( false );

notificationManager.cancel(NOTIFICATION_ID);

Ian Gough answered 2019-08-12T05:54:26Z

0 votes

所有通知(甚至是其他应用通知)都可以通过收听&#39; NotificationListenerService&#39; 如NotificationListenerService Implementation中所述

在服务中,您必须致电cancelAllNotifications()。

必须通过以下方式为您的应用程序启用该服务:

'应用程序&amp; 通知' - &gt; '特殊应用访问' - &gt; '通知访问'。

Samantha answered 2019-08-12T05:55:14Z

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值