java.lang.IllegalArgumentException: Service Intent must be explicit

意思是服务必须得显式的调用

我之前是这样使用绑定Service的

他报错了

因为在5.0之后google升级了SDK

他要求所有的Service服务必须得显式的调用,不能隐式的调用

解决办法 使用如下代码

    /***
* 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;
}

加入新代码转化函数后的用法 这样即可以解决问题

       Intent intent = new Intent();
intent.setAction("actionname.aidl.bank.BankService"); Intent intent1 = new Intent(createExplicitFromImplicitIntent(context,intent));
bindService(intent1,conn,BIND_AUTO_CREATE);

java.lang.IllegalArgumentException: Service Intent must be explicit 解决办法的更多相关文章

  1. java.lang.IllegalArgumentException: Service Intent must be explicit: Intent

    在Activity中启动Service的时候报错: 服务意图必须是显性声明. 这是为了防止造成冲突(i.e. 有多个Service用同样的intent-filter的情况) 这是Android 5.0 ...

  2. Android用Intent来启动Service报“java.lang.IllegalArgumentException: Service Intent must be explicit”错误的解决方法

    今天没事来写个播放器,照搬书上的原句,其中一句 //用于启动和停止service的Intent final Intent it = new Intent("android.mu.action ...

  3. java.lang.IllegalArgumentException: Service not registered

    java.lang.IllegalArgumentException: Service not registered 首先检查一下,Service是否在AndroidManifest文件中注册.格式如 ...

  4. [Android]Caused by: java.lang.IllegalArgumentException: Service not registered.md

    Caused by: java.lang.IllegalArgumentException: Service not registered: org.diql.aidldemo.MainActivit ...

  5. java.lang.IllegalArgumentException: The observer is null.终于解决方式

    java.lang.IllegalArgumentException: The observer is null.终于解决方式 在使用数据适配的时候的问题: java.lang.IllegalArgu ...

  6. SpringBoot整合Swagger2案例,以及报错:java.lang.NumberFormatException: For input string: ""原因和解决办法

    原文链接:https://blog.csdn.net/weixin_43724369/article/details/89341949 SpringBoot整合Swagger2案例 先说SpringB ...

  7. 关于java.lang.NoSuchMethodError: android.widget.RelativeLayout.setBackground的解决办法

    今天用一个安卓4.0.4版本的手机测试手上的项目,发现logcat弹出这样一个提示“java.lang.NoSuchMethodError: android.widget.RelativeLayout ...

  8. 关于java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream解决办法

    吉林的一个项目有个错误找了一天,有段报错是:   java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream 1.遇到过两次,第 ...

  9. IDEA里运行代码时出现Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger的解决办法(图文详解)

    不多说,直接上干货! 问题详情 运行出现log4j的问题 -classpath "C:\Program Files\Java\jdk1.8.0_66\jre\lib\charsets.jar ...

随机推荐

  1. Java.util.properties读取配置文件分析

    Java.util.properties API链接: https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html Clas ...

  2. docker版本升级

    docker的版本变化: Docker从1.13.x版本开始,版本分为企业版EE和社区版CE,版本号也改为按照时间线来发布,比如17.03就是2017年3月,有点类似于ubuntu的版本发布方式. 企 ...

  3. 【系列教程1】Gradle入门系列一:简介

    Gradle是一种构建工具,它抛弃了基于XML的构建脚本,取而代之的是采用一种基于Groovy的内部领域特定语言.近期,Gradle获得了极大的关注. 这篇文章是Gradle教程的第一篇,我们有两个目 ...

  4. oracle 之 统计函数、子查询、操作符

    统计函数 COUNT():查询表中的数据记录 AVG():求出平均值 SUM():求和 MAX():求出最大值 MIN():Q求出最小值 子查询   [单行多列] select * from a wh ...

  5. oracle单行函数 之 转换函数

    to_char(字符串 \ 列, 格式字符串):将日期或者数字变成为字符串显示 注意点:时间字符串或时间类型列  与  格式字符串  必须是一一对应,若是多了少了相关字符会报错(除了使用systemd ...

  6. C# this.Invoke和this.BeginInvoke 最简单的写法

    https://blog.csdn.net/gtosky4u/article/details/20118813 this.BeginInvoke(new EventHandler(delegate { ...

  7. (zhuan) 126 篇殿堂级深度学习论文分类整理 从入门到应用

    126 篇殿堂级深度学习论文分类整理 从入门到应用 | 干货 雷锋网 作者: 三川 2017-03-02 18:40:00 查看源网址 阅读数:66 如果你有非常大的决心从事深度学习,又不想在这一行打 ...

  8. Unity3D学习笔记(三十):Lua

    Lua:是一个轻量级的脚本语句.不需要编译,直接运行的语言.   环境配置:   执行Lua有两种方式: 1.交互式编程(输入后能立即看到效果) 2.脚本式编程:运行lua文件,.lua为文件后缀   ...

  9. .net 打包下载

      ZipArchive 打包下载 private IActionResult DownloadZipFromUrl(string[] guids,string zipFullName) { usin ...

  10. JS基础---常见的Bom对象

    BOM(Browser Object Mode)浏览器对象模型,是Javascript的重要组成部分.它提供了一系列对象用于与浏览器窗口进行交互,这些对象通常统称为BOM. 一张图了解一下先 1.wi ...