NotificationListenerService失效的两种情况
来源:互联网 发布:淘宝电信手机 编辑:程序博客网 时间:2024/06/05 05:57
1、在NotificationListenerService onCreate或Onbind过程中crash。这种情况只能重启手机,即使修改了crash部分的代码重新安装程序也一样不能收到通知。原因是NLS在registerListenerService时会有一个tag标记(可能是为了防止一个service多次启动)bind前会加tag,bind成功结束后会去掉tag。那么在bind时cash标签不会去掉,就导致NLS无效,即使想重新注册,因为有一个tag存在,也会直接return。
private void registerListenerService(final ComponentName name, final int userid) { //servicesBindingTag可以理解为需要启动的service的标签 final String servicesBindingTag = name.toString() + "/" + userid; //如果mServicesBinding中已经包含正在处理的service则直接return退出 if (mServicesBinding.contains(servicesBindingTag)) { // stop registering this thing already! we're working on it return; } //将准备启动的service标签添加到mServicesBinding中 mServicesBinding.add(servicesBindingTag); //... ...省略 //使用bindServiceAsUser启动service Intent intent = new Intent(NotificationListenerService.SERVICE_INTERFACE); intent.setComponent(name); intent.putExtra(Intent.EXTRA_CLIENT_LABEL, R.string.notification_listener_binding_label); intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity( mContext, 0, new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS), 0)); try { if (DBG) Slog.v(TAG, "binding: " + intent); if (!mContext.bindServiceAsUser(intent, new ServiceConnection() { INotificationListener mListener; @Override public void onServiceConnected(ComponentName name, IBinder service) { synchronized (mNotificationList) { //服务成功启动之后删除标签 mServicesBinding.remove(servicesBindingTag); try { mListener = INotificationListener.Stub.asInterface(service); NotificationListenerInfo info = new NotificationListenerInfo( mListener, name, userid, this); service.linkToDeath(info, 0); mListeners.add(info); } catch (RemoteException e) { // already dead } } } @Override public void onServiceDisconnected(ComponentName name) { Slog.v(TAG, "notification listener connection lost: " + name); } }, Context.BIND_AUTO_CREATE, new UserHandle(userid))) { //绑定服务失败后删除标签 mServicesBinding.remove(servicesBindingTag); Slog.w(TAG, "Unable to bind listener service: " + intent); return; } } catch (SecurityException ex) { Slog.e(TAG, "Unable to bind listener service: " + intent, ex); return; } } }
2、被第三方软件杀掉或者service被系统杀掉,可以通过重新绑定服务使NLS重新生效。可以通过adb shell dumpsys notification命令查看手机里enable和live的notificationlistener。如果live里没有说明你的服务没有存活,也就是NLS的无效。可以通过一个函数来重新绑定:
public static void toggleNotificationListenerService(Context context) { Log.e(TAG,"toggleNLS"); PackageManager pm = context.getPackageManager(); pm.setComponentEnabledSetting( new ComponentName(context, com.zhenglei.launcher_test.qianghongbao.NotificationService.class), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); pm.setComponentEnabledSetting( new ComponentName(context, com.zhenglei.launcher_test.qianghongbao.NotificationService.class), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); }
以上参考:点击打开链接
本文只做整理
0 0
- NotificationListenerService失效的两种情况
- 迭代器失效的几种情况总结
- [IOS]几种按钮失效的情况
- mysql索引失效的几种情况
- MySQL索引失效的几种情况
- MySQL索引失效的几种情况
- vector迭代器失效的几种情况
- 索引失效的几种情况
- 索引失效的情况
- 索引失效的情况
- 索引失效的情况
- NotificationListenerService
- 两种情况的splitter
- git 两种情况的
- STL迭代器失效的情况
- Git .gitignore失效的情况
- 导致索引失效的情况
- mysql索引失效的情况
- ISDB-T DVB-T DTV ATV
- mybatis-generator 自动生成带中文注释方法(附实体类)
- ibatis
- Qt浅谈之二十二Qt样式表
- DAVINCI DM6446 开发攻略——V4L2视频驱动和应用分析
- NotificationListenerService失效的两种情况
- Java转型(向上或向下转型)
- OkHttp请求json数据
- 64位32位windows7安装python34和python27,numpy,scipy,matplotlib,scikit-learn等第三方模块
- Swift 之 UISlider
- css解决input里面光标位置靠上问题
- unsigned char与char 转换
- Alignment of Code (代码对齐)UVA1593
- 报错问题 关于在框架中 jar包冲突