关于NotificationListenerService监听时有失败的处理

  • 问题由来 
    去年进入一家专业做智能穿戴设备的公司,在项目中需要监听系统通知栏变化(主要是IM类app的信息获取到后推送到用户的手环),在继承Android系统提供的NotificationListenerService这个类使用时会出现一个问题:应用进程被杀后再次启动时,服务不生效,导致通知栏有内容变更,服务无法感知

  • 解决方法

    • 第一种方法是:重启手机,但是用户体验不好
    • 第二种方法是:在app每次启动时检测NotificationListenerService是否生效,不生效重新开启
  • 代码

public class NotificationCollectorMonitorService extends Service {
@Override
public void onCreate() {
super.onCreate();
ensureCollectorRunning();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
//确认NotificationMonitor是否开启
private void ensureCollectorRunning() {
ComponentName collectorComponent = new ComponentName(this, NotificationMonitor.class);
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
boolean collectorRunning = false;
List<ActivityManager.RunningServiceInfo> runningServices = manager.getRunningServices(Integer.MAX_VALUE);
if (runningServices == null ) {
return;
}
for (ActivityManager.RunningServiceInfo service : runningServices) {
if (service.service.equals(collectorComponent)) {
if (service.pid == Process.myPid() ) {
collectorRunning = true;
}
}
}
if (collectorRunning) {
return;
}
toggleNotificationListenerService();
}
//重新开启NotificationMonitor
private void toggleNotificationListenerService() {
ComponentName thisComponent = new ComponentName(this, NotificationMonitor.class);
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(thisComponent, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(thisComponent, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); }
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
  • 代码说明

    • NotificationMonitor为继承NotificationListenerService的具体监听通知的处理类
    • 在Application的 onCreate方法中启动NotificationCollectorMonitorService
startService(new Intent(this, NotificationCollectorMonitorService.class))
  • 不要忘记了在 AndroidManifest.xml注册此服务
    <service android:name=".NotificationCollectorMonitorService"/>

关于NotificationListenerService监听时有失败的处理的更多相关文章

  1. Oracle监听启动失败问题

    1. C:\Users\10188535.ZTE>lsnrctl start LISTENER LSNRCTL for 32-bit Windows: Version 11.2.0.1.0 - ...

  2. Always On 集群监听创建失败问题

    创建SQL Server Always On监听失败如下 需要增加:1.登陆服务器的域账号创建计算机对象的权限,2.集群名称账号在域环境中创建计算机对象的权限 如果是重建监听:需要3.先删除原先的监听 ...

  3. 问题1:Oracle数据库监听启动失败(重启监听,提示The listener supports no services)

    编辑监听文件:/home/DB/oracle/11gR2/db/network/admin/listener.ora 在文件内添加静态监听实例,如下内容: SID_LIST_LISTENER =(SI ...

  4. Android 通过系统使用NotificationListenerService 监听各种Notification的用法

    NotificationListenerService是通过系统调起的服务,当有应用发起通知的时候,系统会将通知的动作和信息回调给NotificationListenerService. 在继承Not ...

  5. [转]Apache 监听端口失败,selinux惹的祸

    原文在此 CentOS 下启动Httpd 失败,报 (13)Permission denied: make_sock: could not bind to address [::]:8000 因为 小 ...

  6. 怎样监听HTTP请求的成功、失败与进行时

    1. 监听请求成功: xhr.onload 2. 监听请求失败: xhr.onerror 3. 监听请求数据下载中: xhr.onprogress xhr.onload = function() { ...

  7. 【iOS】7.4 定位服务->2.1.3.3 定位 - 官方框架CoreLocation 功能3:区域监听

    本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...

  8. Linux中安装Oracle11g后出现监听的问题及解决办法

    软件安装: 参考文章: linux安装Oracle11G 错误如下: [oracle@iz2f570bi1k56uz admin]$ lsnrctl start LSNRCTL for Linux: ...

  9. Linux下监听或绑定(bind)843端口失败

    问题:写了一个程序,尝试在843端口监听,结果在执行bind的时候失败了 原来,系统不允许用户程序在1-1024端口监听,因为他们是知名端口. 解决办法: 换成root用户,即可成功bind.(ubu ...

随机推荐

  1. Qt开发之Hello Qt及学习小技巧

    创建第一个Qt程序的简单流程 如果安装了andriod或ios的版本也会显示出来,这里只显示了桌面端的 一个项目文件代码结构如下: 如下图可直接编辑label里的文字内容: 运行结果:(快捷键ctrl ...

  2. OneZero第四周第五次站立会议(2016.4.15)

    1. 时间: 15:00--15:15  共计15分钟. 2. 成员: X 夏一鸣 * 组长 (博客:http://www.cnblogs.com/xiaym896/), G 郭又铭 (博客:http ...

  3. 重启Hbase命令

    注意先启动hadoop,记得重启zookeeper. 具体操作如下: cd hadoop-2.7.4/sbin/ && ./stop-all.sh && ./start ...

  4. centos7 登陆报错 grep:write error

    出现这个原因是因为磁盘空间满了 通过df -h查看存储空间 发现磁盘空间满了,可以用 find / -type f -size +1000M 查找大于1000M的文件删除 然后找到用rm -rf 命令 ...

  5. 页面用一个遮罩层显示加载,加载完后隐藏该div

    <div id="background" class="background" style="display: none; "> ...

  6. MongoDB-管道与聚合(3)

    分组:$group()    db.集合.aggregate(       {$group: {_id:'$分组字段名', 显示字段:{$统计函数: '$统计字段'}}},     ) -- 统计男生 ...

  7. [代码]--ORA-01843: 无效的月份

    1.插入的日期如果是DateTime类型的,没有影响 2.如果DateTime.ToString()获取的日期,就会报错,例如(@param_datetime = cf.GetServerDateTi ...

  8. 关于链表的总结(C++循环实现)

    0.目录 1.链表的基本操作 1.1 结点定义 1.2 创建链表 1.3 销毁链表 1.4 打印链表 1.5 获取链表长度 2.结点的基本操作 2.1 删除结点 2.2 查找结点 3.面试题 3.1 ...

  9. 数位DP复习小结

    转载请注明原文地址http://www.cnblogs.com/LadyLex/p/8490222.html 之前学数位dp的时候底子没打扎实 虚的要死 这次正好有时间……刷了刷之前没做的题目 感觉自 ...

  10. Educational Codeforces Round 14 D. Swaps in Permutation(并查集)

    题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 ...