android-Service_Intent_must_be_explicit的解决方法

android: Service Intent must be explicit 的解决方法

在使用AIDL隐式开启一个服务的时候,可能会遇到这个错误

IllegalArgumentException: Service Intent must be explicit

原因是 android 5.0 以后,Service 必须以显式方式启动:

image-20221107135511864

解决方法

从源码上看,我们有两种解决上述问题的办法:

  • 第一种:用显式意图替换隐式意图

    Intent mIntent = new Intent();
    mIntent.setAction("XXX.XXX.XXX");
    Intent intent = new Intent(getExplicitIntent(mContext,mIntent));
    context.startService(intent );
    
  • 第二种:设置packageName

    Intent mIntent = new Intent();
    mIntent.setAction("XXX.XXX.XXX");
    mIntent.setPackage("XXX.XXX.XXX");//Service所在应用的包名
    context.startService(mIntent);
    

举例

报错的写法:

Intent intent = new Intent("com.hansion.aidls.HaHaService");
bindService(intent,mConnection,Context.BIND_AUTO_CREATE);

使用第二种方法解决的例子:

Intent intent = new Intent();
intent.setAction("com.hansion.aidls.HaHaService");
intent.setPackage("com.hansion.aidls");
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

参考链接
1.Service Intent must be explicit 的解决方法
2.Service Intent must be explicit的解决方案



如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
posted @   夜行过客  阅读(450)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
历史上的今天:
2016-11-07 新浪微博客户端(21)-获取当前微博未读数并提示用户
2016-11-07 iOS-解决iOS8及以上设置applicationIconBadgeNumber报错的问题
2016-11-07 新浪微博客户端(20)-集成MJRefresh
2016-11-07 Android Studio-导入External Libraries
点击右上角即可分享
微信分享提示