【转】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调用远 ...
随机推荐
- 从此走上一条iOS程序猿不归路。。。
新的城市,新的生活!前不久刚刚结束了苦逼的面试找工作之旅,期间也小有收货,如今正处年底工作闲暇之余,将前一阵子陆陆续续的总结整理了一下,本人菜鸟程序猿一只,水平有限,本文总结的知识不算深入,比较浅显, ...
- css样式-表格优化
1.表格的初步优化 index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8&qu ...
- nginx本地的测试环境添加SSL
要在本地添加SSL,首先要做的是防火墙是不是放开了443端口,同时,在nginx安装时是不是支持了ssl模块,这个安装网上很容易找到相关资料 防火墙,个人还是用iptables比较直观 先将selin ...
- 【winform程序】自定义webrowser控件调用IE的版本
修改注册表: bit: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROW ...
- mysql分表方法-----MRG_MyISAM引擎分表法
一般来说,当我们的数据库的数据超过了100w记录的时候就应该考虑分表或者分区了,这次我来具体说说分表的一些方法.眼下我所知道的方法都是MYISAM的,INNODB怎样做分表而且保留事务和外键,我还不是 ...
- repo简介
高通的Android JellyBean库已经建立完成,目前已经可以正常访问,可以正常提交.Repo获取代码注意:1. 首先应有一个repo脚本,并将脚本放到环境变量里,此脚本的配置方法a) 进入用户 ...
- overcast
关于一个利用relative的简单布局,firefox上出现一点问题,暂且不明原因 firefox的 chrome的 代码记录 <!DOCTYPE html> <html lang= ...
- 使用 Intel HAXM 为 Android 模拟器加速,媲美真机(转)
源:http://www.cnblogs.com/beginor/archive/2013/01/13/2858228.html 慢的问题. Intel HAXM (Hardware Accelera ...
- Kinect研究
1. 深度照相机 红外 + 2个摄像头. (1彩色摄像头,1红外发射,1接收) 红外捕捉深度. 2. OpenNI 某编程社区创建. 提供基本深度数据的访问.LGPL协议. 用户跟踪付费. 3. 问题 ...
- 武汉科技大学ACM :1009: 零起点学算法63——弓型矩阵
Problem Description 输出n*m的弓型矩阵 Input 多组测试数据 每组输入2个整数 n和m(不大于20) Output 输出n*m的弓型矩阵,要求左上角元素是1,(每个元素占2个 ...