原文网址: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. Generating SSH Keys [Ubuntu Linux]

    Generating SSH Keys We strongly recommend using an SSH connection when interacting with GitHub. SSH ...

  2. Jquery css函数用法(判断标签是否拥有某属性)

    判断一个层是否隐藏:$("#id").css("display")=="none"  ;在所有匹配的元素中,设置一个样式属性的值:$(&qu ...

  3. JavaScript 自动分页插件 datatables

    DataTables Table plug-in for jQuery https://www.datatables.net/

  4. SharePoint 要一个多行文本类型字段为特殊类型的链接

    1.插入在测试列表中的多行文本字段.名字叫做Content.例如下面的附图: 2.在Content字段里.加入一个Link.例如以下图: 3.尝试输入Notes格式的Link,例如以下图: 4.点击O ...

  5. 将apk文件添加到Android模拟器(AVD)中运行

    apk不同exe和jar文件,apk需要在安卓系统中运行,单有一个apk文件还是没用,不能直接拖进AVD中(当然可以直接放到安卓系统的手机中) 由于我们的eclipse大都是已经安装好,解压直接使用的 ...

  6. Word03-文档中的截图显示不全

    今天写文档时遇到个很蛋疼的问题,在doc文档中复制进去的截图总是显示不全,图片上半部分都被文字遮盖了,折腾半天,最后还是网上找到了答案. 解决方法如下: 将图片所在行的段落行距修改为其它值即可,原来为 ...

  7. C# Interface显式实现和隐式实现

    c#中对接口的实现方式有两种:隐式实现和显式实现,之前一直没仔细看过,今天查了些资料,在这里整理一下. 隐式实现的例子 interface IChinese { string Speak(); } p ...

  8. javascript 实现jsonp

    jsonp原理其实也简单,虽然ajax不能跨域,但是通过src这个属性我们可以实现跨域,其实和我们引入第三方jquery调用它的方法一样的. html: <!DOCTYPE html> & ...

  9. Android --------- 压缩图片的尺寸和大小

    压缩图片大小,尺寸不变 将已知路径的图片压缩至不大于目标大小,并保存至指定路径 /** * 质量压缩,通过给定的路径来压缩图片并保存到指定路径 * * @param srcPath * 资源图片的路径 ...

  10. displaytag 添加超链接

    方法1:在<display:column>中使用属性autolink="true",不过这条属性只对两种情况起作用:网址和邮箱,如果只是普通字符串,那么还是普通字符串, ...