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调用远 ...
随机推荐
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Grunt教程——初涉Grunt
前端自动化,这样的一个名词听起来非常的有吸引力,向往力.当今时代,前端工程师需要维护的代码变得及为庞大和复杂,代码维护.打包.发布等流程也 变得极为繁琐,同时浪费的时间和精力也越来越多,当然人为的错误 ...
- snowflake
snowflake在分布式系统中生成全局id
- HDU 2089 数位dp入门
开始学习数位dp...一道昨天看过代码思想的题今天打了近两个小时..最后还是看了别人的代码找bug...(丢丢) 传说院赛要取消 ? ... 这么菜不出去丢人也好吧~ #include<stdi ...
- HTTP 笔记与总结(2 )HTTP 协议的(请求行的)请求方法 及 (响应行的)状态码
(请求行的)请求方法 包括: GET,POST,HEAD,PUT,TRACE,DELETE,OPTIONS 注意:这些请求方法虽然是 HTTP 协议规定的,但是 Web Server 未必允许或支持这 ...
- PROTEL99SE封装说明
PROTEL99SE封装说明 路板上的焊盘间的距离也就是300mil(因为在电机领域里,是以英制单位为主的.同样的, 对于无极性的电容,RAD0.1-RAD0.4 也是一样:对有极性的电容如电解电容 ...
- P1541 乌龟棋
30分做法,暴力枚举: #include <bits/stdc++.h> using namespace std; const int maxn = 400; int n, m; int ...
- 关于C语言读取多行数据的问题
我有如下数据格式3360 2001 5 1750.5 1246.5 22.5 n 1775=1177-1316:13360 2001 5 1750.5 1 ...
- java 开发, jdk 1.6 官方下载地址
在oracle官方网站默认下载的jdk是最新的,目前正式版是1.8. 但有些项目要求是1.6的jdk,费了九牛二虎之力终于找到了1.6的官方版本,链接如下: http://www.oracle.com ...
- .Net Install类的Install、Commit等事件触发顺序
.Net Install类的Install.Commit等事件触发顺序 空间 首先是Install其中调用base.Install过程中导致OnBeforeInstallOnAfterInstal ...