【转】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调用远 ...
随机推荐
- C++中输出流运算符的重载
cout是ostream类的对象,cin是istream类的对象. 我们平时用的cout<<就相当于cout.operator<<(...).也就是说正常使用(不对operat ...
- POJ-1488(字符串应用)
Description TEX is a typesetting language developed by Donald Knuth. It takes source text together w ...
- Java 截取反斜杠--java使用split拆分特殊字符
Java 截取反斜杠 replaceAll和split (“\”) 问题解决办法 xxx.split("\\") 显然得不到想要的结果 正确方法 xxx.split("\ ...
- webpack之基础学习
webpack工作原理: 通过一个入口文件,main.js开始找到你的项目的所有依赖文件,使用loaders处理它们,最后打包为一个浏览器可识别的JavaScript文件. Webpack的核心原理 ...
- linux自己主动重新启动tomcat脚本
0.个人标记 caicongyang http://blog.csdn.net/caicongyang 1.脚本retomcat.sh #!/bin/sh pid=`ps aux |grep tomc ...
- DataGrid( 数据表格) 组件[9]
本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于Panel(面板).Resizeable(调整大小).LinkButton(按钮).Pageination( ...
- ASP.NET快速学习方案(.NET菜鸟的成长之路)
想要快速学习ASP.NET网站开发的朋友可以按照下面这个学习安排进度走.可以让你快速入门asp.net网站开发!但也局限于一般的文章类网站!如果想学习更多的技术可以跟着我的博客更新走!我也是一名.NE ...
- script 表单验证
表单验证:一.非空验证:1.内容是不是空的.判断值的长度是不是0.length属性.压缩空格的函数. 2.内容是不是改变了. 二.对比验证:1.验证两个控件值的关系(相同,大小) 2.验证控件的值与某 ...
- 国外.net学习资源网站
转载 :出处:http://www.cnblogs.com/kingjiong/ 名称:快速入门地址 http://chs.gotdotnet.com/quickstart/ 描述:本站点是微软.NE ...
- u盘启动盘制作工具
u盘启动盘制作工具http://www.dabaicai.biz/ 系统镜像文件下载:http://xt.qingdiangongsi.cn/xtxz/