原文网址:http://blog.csdn.net/shenzhonglaoxu/article/details/42675287

今天在学习android的Service组件的时候,在AndroidMainfest.xml中定义了

  1. <service
  2. android:name=".BindService"
  3. android:enabled="true"
  4. android:exported="true" >
  5. <intent-filter>
  6. <action android:name="com.example.user.firstapp.FIRST_SERVICE"/>
  7. </intent-filter>
  8. </service>

然后在activity中用如下代码绑定service:

  1. final Intent intent = new Intent();
  2. intent.setAction("com.example.user.firstapp.FIRST_SERVICE");
  3. bindService(intent,coon,Service.BIND_AUTO_CREATE);

这时候会报错:

IllegalArgumentException: Service Intent must be explicit

经过查找相关资料,发现是因为Android5.0中service的intent一定要显性声明,当这样绑定的时候不会报错。

  1. final Intent intent = new Intent(this,BindService.class);
  2. bindService(intent,coon,Service.BIND_AUTO_CREATE)

http://blog.android-develop.com/2014/10/android-l-api-21-javalangillegalargumen.html上看到一个解决方法,可以将隐性调用变成显性调用。先定义一个函数:

  1. /***
  2. * Android L (lollipop, API 21) introduced a new problem when trying to invoke implicit intent,
  3. * "java.lang.IllegalArgumentException: Service Intent must be explicit"
  4. *
  5. * If you are using an implicit intent, and know only 1 target would answer this intent,
  6. * This method will help you turn the implicit intent into the explicit form.
  7. *
  8. * Inspired from SO answer: http://stackoverflow.com/a/26318757/1446466
  9. * @param context
  10. * @param implicitIntent - The original implicit intent
  11. * @return Explicit Intent created from the implicit original intent
  12. */
  13. public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
  14. // Retrieve all services that can match the given intent
  15. PackageManager pm = context.getPackageManager();
  16. List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
  17. // Make sure only one match was found
  18. if (resolveInfo == null || resolveInfo.size() != 1) {
  19. return null;
  20. }
  21. // Get component info and create ComponentName
  22. ResolveInfo serviceInfo = resolveInfo.get(0);
  23. String packageName = serviceInfo.serviceInfo.packageName;
  24. String className = serviceInfo.serviceInfo.name;
  25. ComponentName component = new ComponentName(packageName, className);
  26. // Create a new intent. Use the old one for extras and such reuse
  27. Intent explicitIntent = new Intent(implicitIntent);
  28. // Set the component to be explicit
  29. explicitIntent.setComponent(component);
  30. return explicitIntent;
  31. }

然后调用

  1. final Intent intent = new Intent();
  2. intent.setAction("com.example.user.firstapp.FIRST_SERVICE");
  3. final Intent eintent = new Intent(createExplicitFromImplicitIntent(this,intent));
  4. bindService(eintent,conn, Service.BIND_AUTO_CREATE);

这样也可以解决问题。

PS:调用本地service是这样的,不知道其他程序隐性调用service时会不会也有类似的问题,待续。

另一种简单点的解决方法可参考另一篇日志

继续上一篇文章,今天发现了新的解决方法,在生命intent的时候同时调用setAction和setPackage方法,这样创建出来的intent就是显性的

  1. final Intent intent = new Intent();
  2. intent.setAction("com.example.user.firstapp.FIRST_SERVICE");
  3. intent.setPackage(this.getPackageName());
  4. bindService(intent,conn,Service.BIND_AUTO_CREATE);

即设置了intent的action之后还要设置service所在的包名,这里是本地调用,所以用getPackageName()方法就可以获取包名。

实测有效。

【转】Service Intent must be explicit的解决方法的更多相关文章

  1. Android Service Intent must be explicit的解决方法

    今天在学习Android的Service组件的时候,在AndroidMainfest.xml中定义了 <service android:name=".BindService" ...

  2. Service Intent must be explicit的解决方法

    今天遇到如标题问题,查阅资料:http://blog.android-develop.com/2014/10/android-l-api-21-javalangillegalargumen.html ...

  3. 如何解决Android 5.0中出现的警告:Service Intent must be explicit

    有些时候我们使用Service的时需要采用隐私启动的方式,但是Android 5.0一出来后,其中有个特性就是Service Intent  must be explitict,也就是说从Lollip ...

  4. java.lang.IllegalArgumentException: Service Intent must be explicit 解决办法

    java.lang.IllegalArgumentException: Service Intent must be explicit 意思是服务必须得显式的调用 我之前是这样使用绑定Service的 ...

  5. 解决Android 5.0中出现的警告:Service Intent must be explicit

    extends:http://www.eoeandroid.com/thread-568853-1-1.html 本帖最后由 469874851 于 2015-3-11 18:15 编辑 有些时候我们 ...

  6. 我的Android进阶之旅------>如何解决Android 5.0中出现的警告: Service Intent must be explicit:

    我的Android进阶之旅-->如何解决Android 5.0中出现的警告: java.lang.IllegalArgumentException: Service Intent must be ...

  7. 我的Android进阶之旅------&gt;怎样解决Android 5.0中出现的警告: Service Intent must be explicit:

    我的Android进阶之旅-->怎样解决Android 5.0中出现的警告: java.lang.IllegalArgumentException: Service Intent must be ...

  8. [Android分享] 如何解决Android 5.0中出现的警告:Service Intent must be explicit

    Android 5.0程序运行报Service Intent must be explicit错误,原因是5.0的service必须显式调用 改成 Intent intent = new Intent ...

  9. AIDL使用绑定启动远程Service出现Service Intent must be explicit: Intent

    Intent intent = new Intent(); intent.setAction("remote.MyRemoteService.Action"); 使用AIDL调用远 ...

随机推荐

  1. Palindrome Permutation II 解答

    Question Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...

  2. RSA算法python实现

    RSA算法是一种非对称加密算法,是现在广泛使用的公钥加密算法,主要应用是加密信息和数字签名.详情请看维基:http://zh.wikipedia.org/wiki/RSA%E5%8A%A0%E5%AF ...

  3. MTM量身定制系统 - 富友ERP产品 - 信息化推动服装行业管理软件升级

    MTM量身定制系统 - 富友ERP产品 - 信息化推动服装行业管理软件升级 国内第一款量体定制管理系统               结合富友软件10年服装行业经验和多家大型量身定制企业管理经验,推出的 ...

  4. C++编程规范之19:总是初始化变量

    摘要: 一切从白纸开始,未初始化的变量是C和C++程序中错误的常见来源.养成在使用内存之前先清除的习惯,可以避免这种错误,在定义变量的时候就将其初始化. 按照C和C++相同的低层高效率传统,通常并不要 ...

  5. hdu 4940 Destroy Transportation system(水过)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4940 Destroy Transportation system Time Limit: 2000/1 ...

  6. FoxOne---一个快速高效的BS框架--WEB控件属性编辑器

    FoxOne---一个快速高效的BS框架--(1) FoxOne---一个快速高效的BS框架--(2) FoxOne---一个快速高效的BS框架--(3) FoxOne---一个快速高效的BS框架-- ...

  7. Java设计模式---(动态)代理模式

    代理设计模式 定义:为其他对象提供一种代理以控制对这个对象的访问. 动态代理使用 java动态代理机制以巧妙的方式实现了代理模式的设计理念. 之前虽然会用JDK的动态代理,但是有些问题却一直没有搞明白 ...

  8. CSharp命名风格

    1.大小写约定 为了区分一个标识符中的多个单词,把标识符中的每个单词的首字母大写.不要用下划线来区分单词,或者在标识符中任何地方使用下划线,有两种方式适合大写标识符的字母: PascalCasing( ...

  9. 去除html页面中按钮在ios中的默认样式,去除select自带的小三角图标

    btn{-webkit-appearance: none;} -webkit-appearance: none也能去掉select下拉列表后面自带的小三角

  10. linux添加JAVA环境变量

    root用户: 1.修改文件vim /etc/profile 添加以下信息: export JAVA_HOME=/home/jdk1..0_79 (这里需要添加自己的JDK安装目录) export C ...