广告
返回顶部
首页 > 资讯 > 移动开发 >Android 监听Notification 被清除实例代码
  • 549
分享到

Android 监听Notification 被清除实例代码

notificationAndroid 2022-06-06 08:06:42 549人浏览 八月长安
摘要

前言  一般非常驻的Notification是可以被用户清除的,如果能监听被清除的事件就可以做一些事情,比如推送重新计数的问题。  正文 private

前言

 一般非常驻的Notification是可以被用户清除的,如果能监听被清除的事件就可以做一些事情,比如推送重新计数的问题。

 正文


 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、注意广播在推送前要注册好

实际使用中发现还是有一点问题,比如Service被Kill掉了,通知栏点击就会没有反应了,这块还需要再考虑一下,比如把Broadcast在AndroidMainfest中监听。

以上就是对Android Notification 事件的资料整理,希望能帮助Android开发的朋友。

您可能感兴趣的文章:Android Notification使用方法详解Android中使用Notification实现状态栏的通知Android 使用AlarmManager和NotificationManager来实现闹钟和通知栏android使用NotificationListenerService监听通知栏消息Android实现Service下载文件,Notification显示下载进度的示例Android种使用Notification实现通知管理以及自定义通知栏实例(示例四)Android Notification的多种用法总结


--结束END--

本文标题: Android 监听Notification 被清除实例代码

本文链接: https://www.lsjlt.com/news/24552.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作