android通知栏Notification点击,取消,清除响应事件
2015-12-30 11:38
826 查看
这次在实现推送需求的时候,要用到android通知栏Notification点击后进入消息页面,因为要实现一个保存推送用户名字的功能,我在点击后处理了这个功能,但是测试发现我点击删除或者滑动清除后这个功能并没有执行,所以才意识到要处理删除和滑动清除的事件:
首先实现一个BroadcastReceiver
public class NotificationBroadcastReceiver extends BroadcastReceiver { public static final String TYPE = "type"; //这个type是为了Notification更新信息的,这个不明白的朋友可以去搜搜,很多 @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); int type = intent.getIntExtra(TYPE, -1); if (type != -1) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancel(type); } if (action.equals("notification_clicked")) { //处理点击事件 } if (action.equals("notification_cancelled")) { //处理滑动清除和点击删除事件 } } }
而注册Notification是这样的:
Intent intentClick = new Intent(this, NotificationBroadcastReceiver.class); intentClick.setAction("notification_clicked"); intentClick.putExtra(NotificationBroadcastReceiver.TYPE, type); PendingIntent pendingIntentClick = PendingIntent.getBroadcast(this, 0, intentClick, PendingIntent.FLAG_ONE_SHOT); Intent intentCancel = new Intent(this, NotificationBroadcastReceiver.class); intentCancel.setAction("notification_cancelled"); intentCancel.putExtra(NotificationBroadcastReceiver.TYPE, type); PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(this, 0, intentCancel, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle(getString(R.string.setting_gcm_title)) .setContentText(message) .setSound(defaultSoundUri) .setContentIntent(pendingIntentClick) .setDeleteIntent(pendingIntentCancel); notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(type /* ID of notification */, notificationBuilder.build()); //这就是那个type,相同的update,不同add
当然不要忘了在AndroidManifest.xml里面注册
<receiver android:name=".gcm.NotificationBroadcastReceiver"> <intent-filter> <action android:name="notification_cancelled"/> <action android:name="notification_clicked"/> </intent-filter> </receiver>
这样就是初步实现了android通知栏Notification点击,取消,清除响应事件
相关文章推荐
- android通知栏Notification点击,取消,清除响应事件
- 【Android】24.0 手机多媒体(二)——通知(Notification)(2):状态栏通知点击事件处理...
- Android Notification通知栏、点击事件、悬浮通知的简单实现
- android webview js交互, 响应webview中的图片点击事件
- Android 设置了ClickableSpan导致的上层View点击事件无法响应解决方案
- android 自定义listview无法响应点击事件OnItemClickListener的原因
- [android篇]textview中片段响应点击事件(SpannableString)
- android 防止多次点击,它会导致事件侦听响应于其他接口
- Android的软件盘在在点击“enter键”后有事件响应
- android webview js交互, 响应webview中的图片点击事件 ,点击(双击)使图片变大,单击退出
- Android EditText中插入图片并响应点击事件
- android webview js交互, 响应webview中的图片点击事件
- android webview js交互, 响应webview中的图片点击事件
- android 设置叠加父级响应点击事件
- Android防止点击过快造成多次响应事件的解决方法
- Android listView 中响应Button点击事件
- Android:GridView的item无法响应点击事件
- Android中ListView响应Item内部点击事件
- android FrameLayout响应了下层view的点击事件
- Notification的滑动清除和点击删除事件