关于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"/>

最新文章

  1. Maven(一)linux下安装
  2. 浅谈DrawerLayout(抽屉效果)
  3. hdoj 5074
  4. Selenium2入门(三)WebDriver API之Get
  5. Html - 幽灵键盘
  6. A Tour of Go Switch evaluation order
  7. 转 Xcode磁盘空间大清理
  8. sql: 生日三个月内有效
  9. Constant is not finite! That&#39;s illegal. constant:inf&#39;
  10. [转] GDB 下 watch的使用
  11. [Java]读取文件方法大全(转)
  12. JVM菜鸟进阶高手之路六(JVM每隔一小时执行一次Full GC)
  13. FusionCharts中图的属性的总结归纳
  14. MySQL 在线更改 Schema 工具
  15. 20165321 实验三 敏捷开发与XP实践
  16. 22.用demo通过点击切换图片路径
  17. Android FragmentActivity 嵌套 Fragment 调用startActivityForResult返回的requestCode错误
  18. UIKit Dynamic主题学习笔记
  19. 多线程编程——ANR
  20. js拦截全局ajax请求

热门文章

  1. SqlServer测试SQL语句执行效率
  2. 打包spring项目遇到的坑 Unable to locate Spring NamespaceHandler for XML schema ……shcema/context 产生的原因及解决方法
  3. Linux下利用json-c从一个json数组中提取每一个元素中的部分字段组成一个新json数组
  4. Java中String直接赋字符串和new String的区别 如String str=new String(&quot;a&quot;)和String str = &quot;a&quot;有什么区别?
  5. MT【105】待定系数算最大最小
  6. asp.net中SQL语句太长,怎么换行写?
  7. [0,x)的随机数
  8. git 创建空提交
  9. node.js浅谈
  10. P2889 [USACO07NOV]挤奶的时间Milking Time