【Android】监听Notification被清除
2015-01-26 10:57
495 查看
前言
一般非常驻的Notification是可以被用户清除的,如果能监听被清除的事件就可以做一些事情,比如推送重新计数的问题。
声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com
正文
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中监听,,,
结束
好久不写博客就不找借口啦
一般非常驻的Notification是可以被用户清除的,如果能监听被清除的事件就可以做一些事情,比如推送重新计数的问题。
声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com
正文
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中监听,,,
结束
好久不写博客就不找借口啦
相关文章推荐
- android之Notification监听系统清除通知栏
- Android 监听Notification 被清除实例代码
- Android Notification之监听系统清除通知栏方法
- Android 监听Notification 被清除实例代码
- Android 通过系统使用NotificationListenerService 监听各种Notification的使用方法
- Android 通过系统使用NotificationListenerService 监听各种Notification的用法
- android EditText监听与判断是否清除
- 【笔记】Android NotificationListenerService监听短信、来电、微信、QQ等通知消息
- Android 自定义Notification通知样式并设置按钮监听.
- android监听Notification
- Android Notification PendingIntent:当前App被kill 掉之后,当前App发送的Notification消息是否会被自动清除掉 ?
- 监听Notification被清除
- android通知栏Notification点击,取消,清除响应事件
- android Notification 点击与移除监听
- Android通知栏监听之NotificationListenerService
- android使用NotificationListenerService监听通知栏消息
- android notification点击与移除监听
- Android 通过系统使用NotificationListenerService 监听各种Notification的使用方法
- Android Notification 使用详解之三:通过服务更新进度通知&在Activity中监听服务进度
- Android—Android中监听EditText文本输入,实现修改用户名,清除密码