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);

最新文章

  1. 第八章 交互技术,8.4 Weex 双11会场大规模应用的秒开实战和稳定性保障(作者:鬼道)
  2. java设计模式(三)--抽象工厂模式
  3. Nginx Location 语法,与简单配置[转]
  4. 提高CSS开发能力的技巧集
  5. Spring Data MongoDB example with Spring MVC 3.2
  6. sql语句的group by与having子句
  7. Python 内置的一些高效率函数用法
  8. 1.5准备CentOS和Nginx环境「深入浅出ASP.NET Core系列」
  9. 小程序 wx.request
  10. c++ sort
  11. 主机配置nginx后如何获取访问者IP
  12. 关于poi导出excel三种方式HSSFWorkbook,SXSSFWorkbook,csv的总结
  13. php 微信公众号接入支付宝支付
  14. 20155235 2016-2017-1 《Java程序设计》第3周学习总结
  15. 设计模式之&mdash;&mdash;Template模板模式
  16. Asp.net控制Tomcat启动关闭的实现方法
  17. python基础01—基础数据类型
  18. C++第4次实验(基础班)—循环结构程序设计
  19. 20145240《Java程序设计》第三周学习总结
  20. 创建weblogic domain

热门文章

  1. MVC 自定义特性(验证用户登录session是否已经过期)
  2. Redis 安装,配置以及数据操作
  3. matlab练习程序(k-means聚类)
  4. shell编程(四)之循环控制语句(for,while,until,break,continue,case)
  5. 【做题】51NOD1753 相似子串——哈希
  6. hihoCoder week7 完全背包
  7. 关于视觉跟踪中评价标准的相关记录(The Evaluation of Visual Tracking Results on OTB-100 Dataset)
  8. CAS实现单点登录SSO执行原理探究超详细
  9. Latex: 添加IEEE论文keywords
  10. transient关键字详解