【转】Service Intent must be explicit的解决方法
原文网址:http://blog.csdn.net/shenzhonglaoxu/article/details/42675287
今天在学习android的Service组件的时候,在AndroidMainfest.xml中定义了
- <service
- android:name=".BindService"
- android:enabled="true"
- android:exported="true" >
- <intent-filter>
- <action android:name="com.example.user.firstapp.FIRST_SERVICE"/>
- </intent-filter>
- </service>
然后在activity中用如下代码绑定service:
- final Intent intent = new Intent();
- intent.setAction("com.example.user.firstapp.FIRST_SERVICE");
- bindService(intent,coon,Service.BIND_AUTO_CREATE);
这时候会报错:
IllegalArgumentException: Service Intent must be explicit
经过查找相关资料,发现是因为Android5.0中service的intent一定要显性声明,当这样绑定的时候不会报错。
- final Intent intent = new Intent(this,BindService.class);
- bindService(intent,coon,Service.BIND_AUTO_CREATE)
在http://blog.android-develop.com/2014/10/android-l-api-21-javalangillegalargumen.html上看到一个解决方法,可以将隐性调用变成显性调用。先定义一个函数:
- /***
- * Android L (lollipop, API 21) introduced a new problem when trying to invoke implicit intent,
- * "java.lang.IllegalArgumentException: Service Intent must be explicit"
- *
- * If you are using an implicit intent, and know only 1 target would answer this intent,
- * This method will help you turn the implicit intent into the explicit form.
- *
- * Inspired from SO answer: http://stackoverflow.com/a/26318757/1446466
- * @param context
- * @param implicitIntent - The original implicit intent
- * @return Explicit Intent created from the implicit original intent
- */
- public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
- // Retrieve all services that can match the given intent
- PackageManager pm = context.getPackageManager();
- List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
- // Make sure only one match was found
- if (resolveInfo == null || resolveInfo.size() != 1) {
- return null;
- }
- // Get component info and create ComponentName
- ResolveInfo serviceInfo = resolveInfo.get(0);
- String packageName = serviceInfo.serviceInfo.packageName;
- String className = serviceInfo.serviceInfo.name;
- ComponentName component = new ComponentName(packageName, className);
- // Create a new intent. Use the old one for extras and such reuse
- Intent explicitIntent = new Intent(implicitIntent);
- // Set the component to be explicit
- explicitIntent.setComponent(component);
- return explicitIntent;
- }
然后调用
- final Intent intent = new Intent();
- intent.setAction("com.example.user.firstapp.FIRST_SERVICE");
- final Intent eintent = new Intent(createExplicitFromImplicitIntent(this,intent));
- bindService(eintent,conn, Service.BIND_AUTO_CREATE);
这样也可以解决问题。
PS:调用本地service是这样的,不知道其他程序隐性调用service时会不会也有类似的问题,待续。
另一种简单点的解决方法可参考另一篇日志。
继续上一篇文章,今天发现了新的解决方法,在生命intent的时候同时调用setAction和setPackage方法,这样创建出来的intent就是显性的
- final Intent intent = new Intent();
- intent.setAction("com.example.user.firstapp.FIRST_SERVICE");
- intent.setPackage(this.getPackageName());
- bindService(intent,conn,Service.BIND_AUTO_CREATE);
即设置了intent的action之后还要设置service所在的包名,这里是本地调用,所以用getPackageName()方法就可以获取包名。
实测有效。
【转】Service Intent must be explicit的解决方法的更多相关文章
- Android Service Intent must be explicit的解决方法
今天在学习Android的Service组件的时候,在AndroidMainfest.xml中定义了 <service android:name=".BindService" ...
- Service Intent must be explicit的解决方法
今天遇到如标题问题,查阅资料:http://blog.android-develop.com/2014/10/android-l-api-21-javalangillegalargumen.html ...
- 如何解决Android 5.0中出现的警告:Service Intent must be explicit
有些时候我们使用Service的时需要采用隐私启动的方式,但是Android 5.0一出来后,其中有个特性就是Service Intent must be explitict,也就是说从Lollip ...
- java.lang.IllegalArgumentException: Service Intent must be explicit 解决办法
java.lang.IllegalArgumentException: Service Intent must be explicit 意思是服务必须得显式的调用 我之前是这样使用绑定Service的 ...
- 解决Android 5.0中出现的警告:Service Intent must be explicit
extends:http://www.eoeandroid.com/thread-568853-1-1.html 本帖最后由 469874851 于 2015-3-11 18:15 编辑 有些时候我们 ...
- 我的Android进阶之旅------>如何解决Android 5.0中出现的警告: Service Intent must be explicit:
我的Android进阶之旅-->如何解决Android 5.0中出现的警告: java.lang.IllegalArgumentException: Service Intent must be ...
- 我的Android进阶之旅------>怎样解决Android 5.0中出现的警告: Service Intent must be explicit:
我的Android进阶之旅-->怎样解决Android 5.0中出现的警告: java.lang.IllegalArgumentException: Service Intent must be ...
- [Android分享] 如何解决Android 5.0中出现的警告:Service Intent must be explicit
Android 5.0程序运行报Service Intent must be explicit错误,原因是5.0的service必须显式调用 改成 Intent intent = new Intent ...
- AIDL使用绑定启动远程Service出现Service Intent must be explicit: Intent
Intent intent = new Intent(); intent.setAction("remote.MyRemoteService.Action"); 使用AIDL调用远 ...
随机推荐
- Palindrome Permutation II 解答
Question Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...
- RSA算法python实现
RSA算法是一种非对称加密算法,是现在广泛使用的公钥加密算法,主要应用是加密信息和数字签名.详情请看维基:http://zh.wikipedia.org/wiki/RSA%E5%8A%A0%E5%AF ...
- MTM量身定制系统 - 富友ERP产品 - 信息化推动服装行业管理软件升级
MTM量身定制系统 - 富友ERP产品 - 信息化推动服装行业管理软件升级 国内第一款量体定制管理系统 结合富友软件10年服装行业经验和多家大型量身定制企业管理经验,推出的 ...
- C++编程规范之19:总是初始化变量
摘要: 一切从白纸开始,未初始化的变量是C和C++程序中错误的常见来源.养成在使用内存之前先清除的习惯,可以避免这种错误,在定义变量的时候就将其初始化. 按照C和C++相同的低层高效率传统,通常并不要 ...
- hdu 4940 Destroy Transportation system(水过)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4940 Destroy Transportation system Time Limit: 2000/1 ...
- FoxOne---一个快速高效的BS框架--WEB控件属性编辑器
FoxOne---一个快速高效的BS框架--(1) FoxOne---一个快速高效的BS框架--(2) FoxOne---一个快速高效的BS框架--(3) FoxOne---一个快速高效的BS框架-- ...
- Java设计模式---(动态)代理模式
代理设计模式 定义:为其他对象提供一种代理以控制对这个对象的访问. 动态代理使用 java动态代理机制以巧妙的方式实现了代理模式的设计理念. 之前虽然会用JDK的动态代理,但是有些问题却一直没有搞明白 ...
- CSharp命名风格
1.大小写约定 为了区分一个标识符中的多个单词,把标识符中的每个单词的首字母大写.不要用下划线来区分单词,或者在标识符中任何地方使用下划线,有两种方式适合大写标识符的字母: PascalCasing( ...
- 去除html页面中按钮在ios中的默认样式,去除select自带的小三角图标
btn{-webkit-appearance: none;} -webkit-appearance: none也能去掉select下拉列表后面自带的小三角
- linux添加JAVA环境变量
root用户: 1.修改文件vim /etc/profile 添加以下信息: export JAVA_HOME=/home/jdk1..0_79 (这里需要添加自己的JDK安装目录) export C ...