博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Android】监听Notification被清除
阅读量:7212 次
发布时间:2019-06-29

本文共 1567 字,大约阅读时间需要 5 分钟。

    
private 
final BroadcastReceiver mBroadcastReceiver = 
new BroadcastReceiver() {
        @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中监听,,,

本文转自博客园农民伯伯的博客,原文链接:,如需转载请自行联系原博主。

你可能感兴趣的文章
LESS CSS 框架简介与使用
查看>>
2014.09线上课堂报名帖:敏捷个人手机应用使用
查看>>
C# 重启exe
查看>>
Web 服务器 之 简易WWW服务器的架设
查看>>
一种电子病历系统软件框架思想
查看>>
轻松破解NewzCrawler时间限制
查看>>
gDebugger 3.1.1 原版+破解
查看>>
C++ 对象的内存布局(上)
查看>>
在Outlook中用VBA导出HTML格式邮件
查看>>
搭建一个免费的,无限流量的Blog----github Pages和Jekyll入门
查看>>
PHP——通过下拉列表选择时间(转)
查看>>
由1433端口入侵,浅谈sqlserver安全 (转)
查看>>
2个YUV视频拼接技术
查看>>
spring data实现自定义的repository实现类,实现跟jpa联通
查看>>
“惊群”,看看nginx是怎么解决它的
查看>>
Theano3.3-练习之逻辑回归
查看>>
利用RGB-D数据进行人体检测 带dataset
查看>>
live555的编译及使用
查看>>
C++builder XE 安装控件 及输出路径
查看>>
优点和阵列的缺点,并且一个链表
查看>>