【转】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调用远 ...
随机推荐
- HER COFFEE夜场代金券【1折】_北京美食团购_360团购导航
HER COFFEE夜场代金券[1折]_北京美食团购_360团购导航 HER COFFEE夜场代金券
- Nmon 监控 Linux 的系统性能
Nmon(得名于 Nigel 的监控器)是IBM的员工 Nigel Griffiths 为 AIX 和 Linux 系统开发的一款计算机性能系统监控工具.Nmon 可以把操作系统的统计数据展示在屏幕上 ...
- 自己写的Ext树,Ext3.4,静态全部加载
var load = function(){ /** * 书籍资料目录 */ var bookIT = new Ext.tree.TreeNode({ text:"IT", lea ...
- MVC 4.0语法 自动分页
4.0语法中实现自动分页只需要两个方法即可,Skip() ----跳过序列中指定的元素,Take()-----从序列的开头返回指定数量元素. 一般用自动分页都是无刷新的,可以把显示的数据,用局部页面封 ...
- AngularJs学习笔记5——自定义服务
前面整理了AngularJs双向数据绑定和自定义指令的相关内容,从手册上看也知道,ng部分还包括过滤器和函数,以及服务等. 过滤器:filter,就是对数据进行格式化,注意管道格式,例如: {{表达式 ...
- 为什么class中属性以空格分隔?
1 div.contain .blue{color:blue;}/*后代选择器*/2 div.contain.blue{color:blue;} /*多类选择器*/ 以上两种规则分别应用的元素如下: ...
- Xcode简易基础篇,以新手角度去操作
声明:此Newlife XCode非Mac的XCode,避免误会. 日常用的Newlife X组件的相关资源,不限于XCode,只是以XCode组件为主: 1.QQ群:1600800 2.博客 : h ...
- Path对象
Path是连续的Segment的集合,除了 Path 的第一个Segment和最后一个Segment外,其余的Segment的起始点都是前一个Segment的终止点,即Path对象的中的Segment ...
- 打破C++ Const 的规则
从一个C++菜鸟改函数开始 CString MyClass::GetStringValue() const { return m_strValue; } 这个值可能还没有赋值,好吧,那么我先判断是不是 ...
- C/C++语言中const的用法
1. const 在C和C++中的区别 C++中的const正常情况下是看成编译期的常量,编译器并不为const分配空间,只是在编译的时候将期值保存在名字表中,并在适当的时候折合在代码中. 所 ...