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);