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调用远 ...
随机推荐
- IOS第六天(1:scrollView 属性和查看大图)
***查看大图 #import "HMViewController.h" @interface HMViewController () <UIScrollViewDelega ...
- DNS服务器的配置与应用: BIND9 的安装与配置
3. BIND9 的安装与配置 3.1 bind简介 BIND (Berkeley Internet Name Domain)是Domain Name System (DNS) 协议的一个实现,提供了 ...
- POJ 1511 最短路spfa
题很简单 就是有向图中求给出的源点到其余所有点的最短路的和与其余所有点到源点的最短路之和 一开始以为dij对于正权图的单源最短路是最快的 写了一发邻接表的dij 结果超时 把所有的cin改成scanf ...
- PHP 查询时区与设置时区
使用以下语句来查询当前时区设置 date_default_timezone_get() 使用以下语句设置当前时间为北京时间 date_default_timezone_set('PRC');
- 下载站运行广告合作exe文件然后再运行程序文件的bat
有时候下载内容需要接一些静默安装额文件,其实这个代码就是从别的下载站扒来的 @ECHO OFF if not exist ".jb51.exe" GOTO END move &qu ...
- PHP文件操作 之读取一个文件(以二进制只读的方式打开)
最近应用了文件的读取,顺便复习一下! //读取一个文件 $f = fopen($filename,'rb'); $f: 表示返回的一个资源句柄 $filename:要打开的文件路径 rb:参数,表示只 ...
- 127 2016 int
Type Storage Minimum Value Maximum Value (Bytes) (Signed/Unsigned) (Signed/Unsigned) TINYINT 1 -12 ...
- Bluetooth ATT介绍
目录 1 介绍 2 详细内容 2.1 Attribute Type 2.2 Attribute Handle 2.3 Attribute Handle Grouping 2.4 Attribute V ...
- BLE-NRF51822-实现简单扫描器
在sdk目录 XXX:\Keil_v5\ARM\Pack\NordicSemiconductor\nRF_Examples\9.0.0\ble_central 下有官方提供的主从连接的demo. 官 ...
- docker squid---but git proxy should specify by git config --global http.proxy http:...
Usage of loopback devices is strongly discouraged for production use. Either use `--storage-opt dm.t ...