Android 监听Notification 被清除实例代码
前言
一般非常驻的Notification是可以被用户清除的,如果能监听被清除的事件就可以做一些事情,比如推送重新计数的问题。
正文
privatefinalBroadcastReceivermBroadcastReceiver=newBroadcastReceiver(){
@Override
publicvoidonReceive(Contextcontext,Intentintent){
if(intent==null||context==null){
return;
}
mNotificationManager.cancel(NOTIFICATION_ID_LIVE);
Stringtype=intent.getStringExtra(PUSH_TYPE);
if(PUSH_TYPE_LINK.equals(type)){
//mNumLinkes=0;
}elseif(PUSH_TYPE_LIVE.equals(type)){
//mNumLives=0;
}
//这里可以重新计数
}
};
privatevoidsendLiveNotification(){
Intentintent=newIntent(NOTIFICATION_CLICK_ACTION);
NotificationCompat.BuildermBuilder=newNotificationCompat.Builder(this);
Stringtitle="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,newIntent(NOTIFICATION_DELETED_ACTION).putExtra(PUSH_TYPE,PUSH_TYPE_LIVE),0));
mNotificationManager.notify(NOTIFICATION_ID_LIVE,mBuilder.build());
}
代码说明
1、最重要的是setDeleteIntent,这个在APILevel11(Android3.0)新增的
2、注意不要设置setAutoCancel为true,否则监听器接收不到
3、这里统一了点击通知和消除通知的操作
4、注意广播在推送前要注册好
实际使用中发现还是有一点问题,比如Service被Kill掉了,通知栏点击就会没有反应了,这块还需要再考虑一下,比如把Broadcast在AndroidMainfest中监听。
以上就是对AndroidNotification事件的资料整理,希望能帮助Android开发的朋友。
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志