【Android】监听Notification被清除
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null || context == null) {
return;
}
mNotificationManager.cancel(NOTIFICATION_ID_LIVE);
String type = intent.getStringExtra(PUSH_TYPE);
if (PUSH_TYPE_LINK.equals(type)) {
// mNumLinkes = 0;
} else if (PUSH_TYPE_LIVE.equals(type)) {
// mNumLives = 0;
}
// 这里可以重新计数
}
};
private void sendLiveNotification() {
Intent intent = new Intent(NOTIFICATION_CLICK_ACTION);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( this);
String title = "Push测试";
mBuilder.setContentTitle(title);
mBuilder.setTicker(title);
mBuilder.setContentText("https://233.tv/over140");
mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
mBuilder.setSmallIcon(R.drawable.ic_action_cast);
mBuilder.setDefaults(Notification.DEFAULT_ALL);
mBuilder.setWhen(System.currentTimeMillis());
mBuilder.setContentIntent(PendingIntent.getBroadcast( this, NOTIFICATION_ID_LIVE, intent, 0));
mBuilder.setDeleteIntent(PendingIntent.getBroadcast( this, NOTIFICATION_ID_LIVE, new Intent(NOTIFICATION_DELETED_ACTION).putExtra(PUSH_TYPE, PUSH_TYPE_LIVE), 0));
mNotificationManager.notify(NOTIFICATION_ID_LIVE, mBuilder.build());
}
代码说明
1、最重要的是setDeleteIntent,这个在API Level 11(Android 3.0) 新增的
2、注意不要设置setAutoCancel为true,否则监听器接收不到
3、这里统一了点击通知和消除通知的操作
4、注意广播在推送前要注册好
2015-03-28
实际使用中发现还是有一点问题,比如Service被Kill掉了,通知栏点击就会没有反应了,这块还需要再考虑一下,比如把Broadcast在AndroidMainfest中监听,,,
本文转自博客园农民伯伯的博客,原文链接:【Android】监听Notification被清除,如需转载请自行联系原博主。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
【读书笔记】100个Switf必备tips
1、Selector 在Swift中可以用字符串常量来构造Selector,例如: NSNotificationCenter.defaultCenter().addObserver(self,selector: " handleMoviePlayerLoadStateDidChange: ",name:MPMoviePlayerLoadStateDidChangeNotification, object:nil) funchandleMoviePlayerLoadStateDidChange(notification:NSNotification) 注意这个字符串常量最后的那个冒号,如果有一个参数就加1个冒号,没有就不加,多参数则在第一个冒号的后面接上参数名并再加一个冒号,类似于"method:param2:param3:"。 2、使用多元组(Tuple)交换变量的值 funcswapMe<T>(inouta:T,inoutb:T){ (a,b)=(b,a) } 3、"="不支持操作符重载(——好像不是本书的内容,暂时一并写在这一起了) 4、func的参数默认是le...
-
下一篇
Android中文API(134) —— Account
结构 继承关系 public classAccount extendsObjectimplementsParcelabl java.lang.Object android.accounts.Account 类概述 值类型代表在AccountManager中的账号。这个对象实现了Parcelable并且重写了equals(Object)和hashCode(),使得它可以用做Map中的key。 常量 public static finalCreator<Account>CREATOR public final Stringname public final Stringtype 构造函数 publicAccount(String name, String type) publicAccount(Parcelin) 公共方法 public intdescribeContents() 描述各种包含在Parcelable中配置表现的具有特殊意义的对象。 返回值 一个位掩码说明了Parcelable的特殊类型对象的设置。 public booleanequals(Object o) 比...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- CentOS关闭SELinux安全模块
- Windows10,CentOS7,CentOS8安装Nodejs环境
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS8编译安装MySQL8.0.19
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- MySQL数据库在高并发下的优化方案
- Docker使用Oracle官方镜像安装(12C,18C,19C)