Android Service Intent must be explicit的解决方法
今天在学习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, ); // Make sure only one match was found
if (resolveInfo == null || resolveInfo.size() != ) {
return null;
} // Get component info and create ComponentName
ResolveInfo serviceInfo = resolveInfo.get();
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);
这样也可以解决问题。
Android Service Intent must be explicit的解决方法的更多相关文章
- 【转】Service Intent must be explicit的解决方法
原文网址:http://blog.csdn.net/shenzhonglaoxu/article/details/42675287 今天在学习android的Service组件的时候,在Android ...
- 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 ...
- 解决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 ...
- java.lang.IllegalArgumentException: Service Intent must be explicit 解决办法
java.lang.IllegalArgumentException: Service Intent must be explicit 意思是服务必须得显式的调用 我之前是这样使用绑定Service的 ...
- AIDL使用绑定启动远程Service出现Service Intent must be explicit: Intent
Intent intent = new Intent(); intent.setAction("remote.MyRemoteService.Action"); 使用AIDL调用远 ...
随机推荐
- HTML5 挖宝
http://geek.csdn.net/news/detail/91536 http://mozilla.com.cn/thread-360325-1-1.html
- 使用ProxychainsMac下安装及配置
下面几种解决方式 一.先在VPS用composer把Laravel给拖回来,本地你就别想用Composer正常下载Laravel回来了 二.使用全局代理,暂时木有折腾过在终端下怎么折腾,GUI可以使用 ...
- HDU 1698 线段树 区间更新求和
一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...
- 2016.09.14,英语,《Using English at Work》全书笔记
半个月时间,听完了ESLPod出品的<Using English at Work>,笔记和自己听的时候的备注列在下面.准备把每个语音里的快速阅读部分截取出来,放在手机里反复听. 下一阶段把 ...
- Nginx 笔记与总结(5)访问日志管理:计划任务 + 日志切割
要在第二天的凌晨把前一天的访问日志切割备份,并以时间作为文件名,例如:access.20150728.log,这就需要在 Linux 中格式化时间,例如: [root@localhost ~]# da ...
- PHP笔记——java程序员看懂PHP程序
PHP笔记——java程序员看懂PHP程序 php是一种服务器端脚本语言,类型松散的语言. <?php ?> xml风格 <script language=”ph ...
- PHP 魔术变量
PHP 魔术变量 PHP 向它运行的任何脚本提供了大量的预定义常量. 不过很多常量都是由不同的扩展库定义的,只有在加载了这些扩展库时才会出现,或者动态加载后,或者在编译时已经包括进去了. 有八个魔术常 ...
- varnish中忽略cookie进行缓存
varnish不缓存cookie的页面,如果html页面中带有cookie以下代码为接收到结尾的文件,自动去除掉cookiesub vcl_recv { if (req.request == ” ...
- MySQL 5.6.3
SHOW VARIABLES LIKE '%version%'; https://dev.mysql.com/doc/refman/5.6/en/explain.html As of MySQL 5. ...
- Eclipse 安装插件【转】
本文介绍Eclipse插件的安装方法.Eclipse插件的安装方法大体有三种:直接复制.使用link文件,以及使用eclipse自带的图形界面的插件安装方法. AD: 做为当下最流行的开源IDE之一, ...