如何知道我在通知栏的展示的 Notification 被点击了?
来源:互联网 发布:网络推广思维导图 编辑:程序博客网 时间:2024/05/20 12:46
http://blog.csdn.net/z1074971432/article/details/9465939
如何知道我在通知栏的展示的 Notification 被点击了?
在某些业务需求下,我们需要统计发出去的通知被点击的次数,但是察看 Notifycation 和 NotifycationManager 里面都没有相关方法来设置点击监听器!
那怎么办?
Notifycation 本来就需要我们给他设置一个 Intent 来执行点击之后的动作,这个 Intent 通过 PendingIntent 赋予。
关于 PendingIntent 刚接触可能会觉得不理解,官方解释:A description of an Intent and target action to perform with it. 即将要执行的动作
我们先从区别上来看他们:
1.普通的 Intent 我们发出去之后立马就执行了,比如 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parser("http://baidu.com"))) 之后立马就跳到浏览器打开百度首页了
2.Notifycation 发出去之后用户什么时候点击是不确定的,我们先将点击之后要执行的 Intent 保存下来,等到用户点击之后在取出来执行,那用什么来保存 Intent 呢?PendingIntent(注:我这里为了方便理解而这样解释,事实上 PendingIntent 远比保存 Intent 复杂,涉及跨进程)
而,Intent 本身实现了 Parcelable 接口,可以用来跨进程传输,所以我们可以通过 Intent.putExtra("name", intemt) 将真正要执行的 Intent 保存在 clickIntent 对象里!
然后我们使用 clickIntent 来启动 BroadcastReceiver ,(因为 BroadcastReceiver 是一个轻量级的东西,用在这种情况下再适合不过了),看码:
- Intent realIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://wap.mrpej.com")); //真正的 Intent
- realIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- Intent clickIntent = new Intent(this, ClickReceiver.class); //点击 Intent
- clickIntent.putExtra("realIntent", realIntent);
当用户点击 Notifycation 之后,系统调用 ClickReceiver 的 onReceiver() 方法,你在此时执行真正的点击逻辑就OK,看码:
- @Override
- public void onReceive(Context context, Intent intent) {
- Toast.makeText(context, "通知被点击了", Toast.LENGTH_SHORT).show();
- Intent realIntent = intent.getParcelableExtra("realIntent");
- context.startActivity(realIntent);
- }
但是 onReceiver 里面不能执行长时间的任务,类似网络全球这样的工作,那怎么办呢?照理,你在此时启动一个 Service 来处理就OK!
完整代码:
- private void showNotifycation() {
- Notification n = new Notification(android.R.drawable.stat_notify_chat, "hello I am yichou!", System.currentTimeMillis());
- Intent realIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://wap.mrpej.com"));
- realIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- Intent clickIntent = new Intent(this, ClickReceiver.class);
- clickIntent.putExtra("realIntent", realIntent);
- PendingIntent pi = PendingIntent.getBroadcast(this, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
- n.setLatestEventInfo(this,
- "叙利亚化学武器疑云",
- "叙利亚现总统巴沙尔-阿萨德,是叙已故总统老阿萨德的次子...",
- pi);
- n.flags |= Notification.FLAG_AUTO_CANCEL; //点击一次后自动消除
- NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- nm.notify(1001, n);
- }
注意点:
1.在 onReceiver 的 context 直接启动 Activity 是会报错的,需要给 Intent 加上 Intent.FLAG_ACTIVITY_NEW_TASK 标志!
另外,我使用发送广播的方式也可以进行统计点击阅读的数量:
取出未读数量,发送,
LocalBroadcastManager.getInstance(this).sendBroadcastSync(intent);
- 如何知道我在通知栏的展示的 Notification 被点击了?
- 如何知道我在通知栏的展示的 Notification 被点击了?
- Android -- Notification,如何向通知栏发送通知,点击通知开启相应的Activity
- Notification(Notification的通知栏常驻、Notification的各种样式、Notification点击无效)
- 通知的点击跳转(Notification)
- Notification -- 一点击通知栏的通知,该通知就消失
- 监听自定义的通知栏是否被点击了
- Android的Notification的自定义布局按钮点击+点击按钮收回系统通知栏
- 通知栏通知:Notification的实现
- Notification消息通知 通知栏的实现
- android通知栏的消息通知(Notification)
- Notification通知栏的使用
- Notification通知栏的使用
- 通知栏Notification的整合
- Notification 通知栏的使用
- Notification通知栏的用法
- Android 弹无虚发之第四弹:你应该掌握的Notification(Notification的通知栏常驻、Notification的各种样式、Notification点击无效)
- Android 弹无虚发之第四弹:你应该掌握的Notification(Notification的通知栏常驻、Notification的各种样式、Notification点击无效)
- 广电总局发布TVOS2.0,首批63家成员单位(含乐视致新)
- for和forin区别
- vm硬盘及主板
- Json.NET读写Json文件(转)
- qt控件的使用
- 如何知道我在通知栏的展示的 Notification 被点击了?
- Alex 的 Hadoop 菜鸟教程: 第12课 Sqoop1 安装/导入/导出教程
- 类与类的四种关系
- arm qt-4.5.0更换为qt4.8.5
- Android细节:OOM,图片,layout
- Reactive-Native学习:iOS工程内嵌React-Native(一)
- angularjsjs require : 'ngModel'
- Android开发笔记(二十九)使用SharedPreferences存取数据
- 随机森林建模