有些时候我们使用Service的时需要采用隐私启动的方式,但是Android 5.0一出来后,其中有个特性就是Service Intent  must be explitict,也就是说从Lollipop开始,service服务必须采用显示方式启动。
而android源码是这样写的(源码位置:sdk/sources/android-21/android/app/ContextImpl.java):

  1. private void validateServiceIntent(Intent service) {
  2. if (service.getComponent() == null && service.getPackage() == null) {
  3. if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
  4. IllegalArgumentException ex = new IllegalArgumentException(
  5. "Service Intent must be explicit: " + service);
  6. throw ex;
  7. } else {
  8. Log.w(TAG, "Implicit intents with startService are not safe: " + service
  9. + " " + Debug.getCallers(2, 3));
  10. }
  11. }
  12. }

既然,源码里是这样写的,那么这里有两种解决方法:

1、设置Action和packageName:

参考代码如下:

  1. Intent mIntent = new Intent();
  2. mIntent.setAction("XXX.XXX.XXX");//你定义的service的action
  3. mIntent.setPackage(getPackageName());//这里你需要设置你应用的包名
  4. context.startService(mIntent);

此方式是google官方推荐使用的解决方法。

在此附上地址供大家参考:http://developer.android.com/goo ... tml#billing-service,有兴趣的可以去看看。

2、将隐式启动转换为显示启动:--参考地址:http://stackoverflow.com/a/26318757/1446466

  1. public static Intent getExplicitIntent(Context context, Intent implicitIntent) {
  2. // Retrieve all services that can match the given intent
  3. PackageManager pm = context.getPackageManager();
  4. List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
  5. // Make sure only one match was found
  6. if (resolveInfo == null || resolveInfo.size() != 1) {
  7. return null;
  8. }
  9. // Get component info and create ComponentName
  10. ResolveInfo serviceInfo = resolveInfo.get(0);
  11. String packageName = serviceInfo.serviceInfo.packageName;
  12. String className = serviceInfo.serviceInfo.name;
  13. ComponentName component = new ComponentName(packageName, className);
  14. // Create a new intent. Use the old one for extras and such reuse
  15. Intent explicitIntent = new Intent(implicitIntent);
  16. // Set the component to be explicit
  17. explicitIntent.setComponent(component);
  18. return explicitIntent;
  19. }

调用方式如下:

  1. Intent mIntent = new Intent();
  2. mIntent.setAction("XXX.XXX.XXX");
  3. Intent eintent = new Intent(getExplicitIntent(mContext,mIntent));
  4. context.startService(eintent);

复制代码

如何解决Android 5.0中出现的警告:Service Intent must be explicit的更多相关文章

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

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

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

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

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

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

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

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

  5. 如何解决Android 5.0以上出现的警告:Service Intent must be expli

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

  6. Android权限管理之RxPermission解决Android 6.0 适配问题

    前言: 上篇重点学习了Android 6.0的运行时权限,今天还是围绕着Android 6.0权限适配来总结学习,这里主要介绍一下我们公司解决Android 6.0权限适配的方案:RxJava+RxP ...

  7. Android 7.0 中 ContentProvider 实现原理

    欢迎大家前往腾讯云社区,获取更多腾讯海量技术实践干货哦~ 作者:汪毅雄 导语: 本文描述了ContentProvider发布者和调用者这两在Framework层是如何实现的. 作为Android的四大 ...

  8. Android 6.0 中的 Wifi 连接

    Android 6.0 中的 Wifi 连接 这几天在写一个软件,结果被其中的 wifi 连接问题困扰了 3 天. 先描述下需求: usb 接口接了一根 usb2serial,通过这个接口接收命令 当 ...

  9. 在Android 5.0中使用JobScheduler

    在Android 5.0中使用JobScheduler 原文链接 : using-the-jobscheduler-api-on-android-lollipop 译者 : Mr.Simple 校对者 ...

随机推荐

  1. servlet上传图片 服务器路径

    1.在servlet中上传图片,上传的文件夹是imge在webroot下,主要代码如下 private void saveImage(HttpServletRequest request, HttpS ...

  2. ADB几种常见的错误及解决方法

    下面列举出几种常见的错误及解决方法. Q1:无效的安装包,安装包已损坏[INSTALL_FAILED_INVALID_APK] A1:请检查安装包是否完整.如果是xpk包,可以通过 手动安装xpk来检 ...

  3. 在mac上访问自带服务器权限问题

    在开发中,有时候我们需要自己的在电脑上做一些网络相关的测试功能,因此,我们必须在本地上模拟网络环境. 在模拟网络环境中,经常会遇到访问权限的问题.现在我就把自己的解决办法写出来.我用的模拟服务器站点是 ...

  4. 魔兽世界---屠夫(Just a Hook)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  5. 一步一步挖出Compute

    前几天在做结账的时候,对数据表DataGridView控件的单列求和纠结了一番.        如今差点儿养成了习惯,对于一些东西疏于開始的思考,不会先想到百度,这里我是先想到了第一版的机房收费那块的 ...

  6. 自己写的操作sql的公共类

    /* /' `\/ `. . .' : `. `. \\.' , `.` `. `. ,___/|\. `. : . \, .'./ ' '\ , ' .\ . \_.~ _; ; \/'. `\ . ...

  7. Android入门——UI(2)

    介绍SeekBar拖动条控件.ProgressBar进度条控件.DatePicker日历控件.TimePicker时间控件 <?xml version="1.0" encod ...

  8. Deep Learning(深度学习)学习笔记整理系列之(四)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  9. BZOJ 4197: [Noi2015]寿司晚宴( dp )

    N^0.5以内的质数只有8个, dp(i, j, k)表示用了前i个大质数(>N^0.5), 2人选的质数(<=N^0.5)集合分别为j, k时的方案数. 转移时考虑当前的大质数p是给哪个 ...

  10. 转载[WampServer下使用多端口访问]

    作者:韩子迟 原文链接:http://www.cnblogs.com/zichi/p/4589142.html 注意点:www和www2都需要安装服务: 在C:\wamp\bin\apache\Apa ...