Android  通知使用权(NotificationListenerService)的使用

发布时间 - 2026-01-11 02:00:50    点击率:

Android  通知使用权(NotificationListenerService)的使用

简介

当下不少第三方安全APP都有消息管理功能或者叫消息盒子功能,它们能管理过滤系统中的一些无用消息,使得消息栏更清爽干净。其实此功能的实现便是使用了Android中提供的通知使用权权限。Android4.3后加入了通知使用权NotificationListenerService,就是说当你开发的APP拥有此权限后便可以监听当前系统的通知的变化,在Android4.4后还扩展了可以获取通知详情信息。下面我们来看看NotificationListenerService的具体使用。

使用

新建一服务类,使它继承NotificationListenerService,并实现两个重要的方法:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) 
public class NotificationListener extends NotificationListenerService { 
  privatestatic final String TAG = "NotificationListener"; 
  
  @Override 
  public void onNotificationRemoved(StatusBarNotification sbn) { 
    Log.i(TAG,"Notification removed"); 
  } 
  
  @Override 
  public void onNotificationPosted(StatusBarNotification sbn) { 
    Log.i(TAG, "Notification posted"); 
  } 
} 

AndroidManifest.xml中声明此服务类,并必须声明BIND_NOTIFICATION_LISTENER_SERVICE许可和意图过滤器

android.service.notification.NotificationListenerService,还有我们在系统设置中通知使用权列表中看到的label标签:

<serviceandroid:name=".NotificationListener" 
     android:label="通知使用权测试程序" 
     android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> 
  <intent-filter> 
    <actionandroid:name="android.service.notification.NotificationListenerService"/> 
  </intent-filter> 
 </service> 

OK,就这么简单就可以完成APP监听系统通知栏的功能了。接下来,我们来看看NotificationListenerService类还提供了一些重要的方法:

StatusBarNotification[] sbns = getActiveNotifications();         // 返回当前系统所有通知的数组 
cancelAllNotifications();                        // 删除系统中所有可被清除的通知 
cancelNotification(String pkg, String tag, int id);           // 删除具体某一个通知 

还有上面我们提到的两个重要的重写方法:

onNotificationRemoved(StatusBarNotification sbn);            // 通知被移除时回调 
onNotificationPosted(StatusBarNotification sbn);             // 增加一条通知时回调 

这两个重要的回调方法它们的参数StatusBarNotification对象是当前触发变化通知的详细信息。来看下StatusBarNotification的使用:

sbn.getId();                               // 返回通知对应的id 
sbn.getNotification();                          // 返回通知对象 
sbn.getPackageName();                          // 返回通知对应的包名 
sbn.getPostTime();                            // 返回通知发起的时间 
sbn.getTag();                              // 返回通知的Tag,如果没有设置返回null 
sbn.isClearable();                            // 返回该通知是否可被清楚,是否为FLAG_ONGOING_EVENT、FLAG_NO_CLEAR 
sbn.isOngoing();                             // 返回该通知是否在正在运行的,是否为FLAG_ONGOING_EVENT 

其中,getNotification()返回的通知对象,还可以详细看到通知的其它相关信息,如:

Notification notification = sbn.getNotification(); 
notification.contentView;                        // 通知的RemoteViews 
notification.contentIntent;                       // 通知的PendingIntent 
notification.actions;                          // 通知的行为数组 
// Android4.4后还扩展了可以获取通知详情信息 
if (Build.VERSION.SDK_INT >Build.VERSION_CODES.JELLY_BEAN_MR2) { 
     Bundle extras = notification.extras; 
     String notificationTitle = extras.getString(Notification.EXTRA_TITLE); 
     int notificationIcon = extras.getInt(Notification.EXTRA_SMALL_ICON); 
     Bitmap notificationLargeIcon = ((Bitmap)extras.getParcelable(Notification.EXTRA_LARGE_ICON)); 
     CharSequence notificationText = extras.getCharSequence(Notification.EXTRA_TEXT); 
     CharSequence notificationSubText = extras.getCharSequence(Notification.EXTRA_SUB_TEXT); 
} 

跳转系统设置里的通知使用权页面

private boolean gotoNotificationAccessSetting(Contextcontext) { 
  try { 
    Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(intent); 
    return true; 
  } catch(ActivityNotFoundException e) { 
    try { 
      Intent intent = new Intent(); 
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      ComponentName cn = new ComponentName("com.android.settings","com.android.settings.Settings$NotificationAccessSettingsActivity"); 
      intent.setComponent(cn); 
      intent.putExtra(":settings:show_fragment", "NotificationAccessSettings"); 
      context.startActivity(intent); 
      return true; 
    } catch(Exception ex) { 
      ex.printStackTrace(); 
    } 
    return false; 
  } 
} 

判断是否拥有通知使用权

private boolean notificationListenerEnable() { 
  boolean enable = false; 
  String packageName = getPackageName(); 
  String flat= Settings.Secure.getString(getContentResolver(),"enabled_notification_listeners"); 
  if (flat != null) { 
    enable= flat.contains(packageName); 
  } 
  return enable; 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


# Android  # 通知使用权NotificationListenerService  # NotificationListenerService实例详解  # Android入门之Service的使用详解  # Android NotificationListenerService通知监听服务使用  # Android Google AutoService框架使用详解  # Android使用Service实现IPC通信的2种方式  # 说说在Android如何使用服务(Service)的方法  # Android使用Service实现简单音乐播放实例  # 浅谈Android中Service的注册方式及使用  # Android编程使用Service实现Notification定时发送功能示例  # Android Service功能使用示例代码  # 回调  # 来看看  # 系统设置  # 都有  # 还可以  # 当你  # 这两个  # 希望能  # 如果没有  # 相关信息  # 重写  # 跳转  # 谢谢大家  # 后便  # 使它  # 移除  # 就可以  # 判断是否  # 正在运行  # 使用了 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: 如何用免费手机建站系统零基础打造专业网站?  如何确认建站备案号应放置的具体位置?  php json中文编码为null的解决办法  韩国代理服务器如何选?解析IP设置技巧与跨境访问优化指南  成都网站制作公司哪家好,四川省职工服务网是做什么用?  小视频制作网站有哪些,有什么看国内小视频的网站,求推荐?  Laravel怎么为数据库表字段添加索引以优化查询  Laravel队列任务超时怎么办_Laravel Queue Timeout设置详解  Laravel distinct去重查询_Laravel Eloquent去重方法  西安市网站制作公司,哪个相亲网站比较好?西安比较好的相亲网站?  详解Android图表 MPAndroidChart折线图  Laravel Seeder怎么填充数据_Laravel数据库填充器的使用方法与技巧  Laravel如何实现数据导出到PDF_Laravel使用snappy生成网页快照PDF【方案】  如何在浏览器中启用Flash_2025年继续使用Flash Player的方法【过时】  详解阿里云nginx服务器多站点的配置  iOS UIView常见属性方法小结  米侠浏览器网页背景异常怎么办 米侠显示修复  EditPlus中的正则表达式实战(6)  网站设计制作书签怎么做,怎样将网页添加到书签/主页书签/桌面?  linux写shell需要注意的问题(必看)  如何在万网主机上快速搭建网站?  高配服务器限时抢购:企业级配置与回收服务一站式优惠方案  Laravel如何使用Service Provider服务提供者_Laravel依赖注入与容器绑定【深度】  如何快速搭建自助建站会员专属系统?  如何在建站之星绑定自定义域名?  如何在局域网内绑定自建网站域名?  如何在服务器上三步完成建站并提升流量?  网站制作软件免费下载安装,有哪些免费下载的软件网站?  ,网页ppt怎么弄成自己的ppt?  Claude怎样写结构化提示词_Claude结构化提示词写法【教程】  再谈Python中的字符串与字符编码(推荐)  如何在万网ECS上快速搭建专属网站?  Laravel如何构建RESTful API_Laravel标准化API接口开发指南  Laravel怎么处理异常_Laravel自定义异常处理与错误页面教程  Laravel的路由模型绑定怎么用_Laravel Route Model Binding简化控制器逻辑  利用python获取某年中每个月的第一天和最后一天  电商网站制作多少钱一个,电子商务公司的网站制作费用计入什么科目?  Laravel如何处理JSON字段_Eloquent原生JSON字段类型操作教程  Laravel如何实现多语言支持_Laravel本地化与国际化(i18n)配置教程  西安专业网站制作公司有哪些,陕西省建行官方网站?  油猴 教程,油猴搜脚本为什么会网页无法显示?  如何基于云服务器快速搭建网站及云盘系统?  Laravel如何自定义分页视图?(Pagination示例)  Firefox Developer Edition开发者版本入口  Laravel如何实现URL美化Slug功能_Laravel使用eloquent-sluggable生成别名【方法】  千库网官网入口推荐 千库网设计创意平台入口  Android Socket接口实现即时通讯实例代码  怎么制作一个起泡网,水泡粪全漏粪育肥舍冬季氨气超过25ppm,可以有哪些措施降低舍内氨气水平?  Laravel如何处理表单验证?(Requests代码示例)  javascript基本数据类型及类型检测常用方法小结