Service Intent must be explicit的解决方法
今天遇到如标题问题,查阅资料: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;
}
一个转换方法,如下方法调用:
private void startMyIntentService() {
Intent startServiceIntent = new Intent("com.soyoungboy.intentservice");
Bundle bundle = new Bundle();
bundle.putString("param", "oper1");
startServiceIntent.putExtras(bundle);
startService(createExplicitFromImplicitIntent(this,startServiceIntent));
}
就可以了
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.csdn.net/shenzhonglaoxu/article/details/42675287 今天在学习android的Service组件的时候,在Android ...
- 如何解决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 ...
- java.lang.IllegalArgumentException: Service Intent must be explicit 解决办法
java.lang.IllegalArgumentException: Service Intent must be explicit 意思是服务必须得显式的调用 我之前是这样使用绑定Service的 ...
- [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调用远 ...
随机推荐
- ROS (Robot Operating System) 相关资料与文档
本博文持续更新…… ROS学习与实践保护很多资料,最权威的当然是官网: www.ros.org 当然也有很多其他博文,尤其是中文文档有助于理解.2017推出一篇新博文,概况的资料较全. http:// ...
- zookeeper_节点数据版本号问题
转自:Simba_cheng 更新节点数据的方法: 同步方法:Stat setData(final String path, byte data[], int version) 异步方法:void s ...
- Linux命令之mount挂载
挂载概念 Linux中的根目录以外的文件要想被访问,需要将其“关联”到根目录下的某个目录来实现,这种关联操作就是“挂载”,这个目录就是“挂载点”,解除次关联关系的过程称之为“卸载”. 注意:“挂载点” ...
- Less 的用法
1. node.js node.js是一个前端的框架 自带一个包管理工具npm node.js 的安装 官网:http://nodejs.cn/ 在命令行检验是否安装成功 切换到项目目录,初始化了一个 ...
- IE10不能显示JSON文件内容
IE7,8,9下Ajax返回后,再执行跳转,会弹出阻止提示框. 所以我采用WebForm 提交思想: //导出 jv.postOpen = jv.PostOpen = jv.Export = func ...
- Centos7 Jenkins
代码上线 持续集成 随时随地将代码合并,这种方法叫做持续集成. 持续集成(CONTINUOUS INTEGRATION,简称CI)持续集成指的是,频繁地(一天多次)将代码集成到主干.它的好处主要有两个 ...
- runlevel 命令详解
基础命令学习目录首页 原文链接:https://blog.csdn.net/PecoVio/article/details/82428883 runlevel 知识扩展 linux操作系统自从开始启动 ...
- linux安装nginx并配置负载均衡
linux上安装nginx比较简单: 前提是需要有gcc或者g++ 1.yum需要的依赖 yum -y install openssl openssl-devel 2.解压pcre库.zlib库 ...
- 【Oracle】存储过程在字符串单引号'内拼接单引号'
http://blog.csdn.net/u011704894/article/details/44976557 一般变量里面接3个单引号 eg: 'DELETE FROM RDM_SUPP_DATA ...
- 原生JavaScript实现的贪吃蛇
github代码地址:https://github.com/McRayFE/snake 涉及到的知识点: 键盘事件 setInterval()定时器 javascript中数组的使用 碰撞的检测 of ...