当前位置: 首页 > news >正文

Android 11 Unable to start/bind service

今天在Android11上发现了一个的问题,如果目标Service的进程没有启动,那么无论是bindService还是startService都没有办法拉起指定的Service。
网上查了很多资料如下:
1.目标Service 设置 android:exported="true"
2.目标Service需要声明自定义权限。客户端需要声明权限。
3.目标Service需要添加<intent-filter></intent-filter>

上面的方法都试过了**然并卵**,还是报Unable to start service Intent.
实在没办法去翻了下源码~在startService中过程中当调用调用pms去解析intent是返回null会打印这个日志。代码片段如下~

ActiveServices # retrieveServiceLocked     ResolveInfo rInfo = mAm.getPackageManagerInternalLocked().resolveService(service,resolvedType, flags, userId, callingUid);ServiceInfo sInfo = rInfo != null ? rInfo.serviceInfo : null;if (sInfo == null) {Slog.w(TAG_SERVICE, "Unable to start service " + service + " U=" + userId +": not found");return null;}


     //....代码省略
    

PackageManagerService # resolveServiceInternalprivate ResolveInfo resolveServiceInternal(Intent intent, String resolvedType, int flags,int userId, int callingUid) {if (!mUserManager.exists(userId)) return null;flags = updateFlagsForResolve(flags, userId, callingUid, false /*includeInstantApps*/,false /* isImplicitImageCaptureIntentAndNotSetByDpc */);List<ResolveInfo> query = queryIntentServicesInternal(intent, resolvedType, flags, userId, callingUid, false /*includeInstantApps*/);if (query != null) {if (query.size() >= 1) {// If there is more than one service with the same priority,// just arbitrarily pick the first one.return query.get(0);}}return null;}


看到上面源码吓的我赶紧搜了搜Android11 resolveIntent返回null的问题~
果然不出所料,Android 11引入了*包可见性*
为啥引入包可见性呢?
Goole给出的原因:
1.鼓励最小权限原则,需要与那些应用交互,就申请那些包名。
2.帮助 Google Play 等应用商店评估应用的隐私性和安全性。、

好啦,知道了前因后果那么我们来解决问题吧~

Solve方案
方案一:

<queries>//你要交互的service的包名<package android:name="com.XXX.XXX" />//...等等包名
</queries>


方案二:

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>


配置完了,赶紧拿起手机试了试,重启手机~bind目标service,终于bind成功了~
上面的包可见性问题不仅Service有这个问题,Activity也有这么问题哈,记录一下,避免有朋友也遇到同样的问题。

http://www.lryc.cn/news/405892.html

相关文章:

  • 走难而正确的路并持之以恒
  • 规范:Redis规范
  • 比较 WordPress 、 Baklib 和 BetterDocs
  • Redis 哨兵搭建
  • HackTheBox--Knife
  • Linux_实现TCP网络通信
  • 正则表达式与文本三剑客之grep
  • 微信小程序开发:项目程序代码构成
  • 【云原生】Kubernetes微服务Istio:介绍、原理、应用及实战案例
  • 【Docker】Docker-consul容器服务自动发现与注册
  • Go 1.22 remote error: tls: handshake failure
  • 迈向通用人工智能:AGI的到来与社会变革展望
  • 大模型额外篇章三:vercel搭建openai中转服务器
  • 使用 jQuery 中的 this 实例
  • 下载最新版Anaconda、安装、更换源、配置虚拟环境并在vscode中使用
  • 极狐GitLab Git LFS(大文件存储)如何管理?
  • 迭代学习笔记
  • 【安全】系统安全设计规范(DOC完整版)
  • windows常用命令整理
  • 视频处理基础知识1
  • Linux退不出vim编辑模式
  • TikTok养号的网络环境及相关代理IP知识
  • 过程调用和数组的分配访问
  • TeamViewer手机端APP提示:请先验证账户
  • 【SpringBoot】分页查询
  • 微软CrowdStrike驱动蓝屏以及内核签名
  • Spring中Bean的循环依赖
  • Java二十三种设计模式-代理模式模式(8/23)
  • Windows 11 家庭中文版 安装 VMWare 报 安装程序检测到主机启用了Hyper-V或Device
  • 机械学习—零基础学习日志(高数09——函数图形)