有些时候我们使用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. python-摩斯码转换

    意义:简单实现摩斯码的破译和生成 代码: #-*- coding: UTF-8 -*- ' __date__ = '2016/2/2' import pprint import re chars = ...

  2. Python 字符串格式化示例

    先看代码吧. #!/usr/bin/env python #-*- coding: utf-8 -*- __author__ = 'jiang' __creattime__ = '2015/10/31 ...

  3. 完美解决IE(IE6/IE7/IE8)不兼容HTML5标签的方法zt

    HTML5的语义化标签以及属性,可以让开发者非常方便地实现清晰的web页面布局,加上CSS3的效果渲染,快速建立丰富灵活的web页面显得非常简单. HTML5的新标签元素有: <header&g ...

  4. MVC自学第一课

    了解传统的ASP.NET WebForm ASP.NET 在02年问世,给Web开发领域带来了巨大转变.下图描述了当时微软的技术堆栈. ASP.NET WebForm 技术堆栈 (注:此图的含义为,W ...

  5. Sql优化(三) 关于oracle的并发

    Oracle的并发技术可以将一个大任务分解为多个小任务由多个进程共同完成.合理地使用并发可以充分利用系统资源,提高效率.一. 并发的种类Parallel queryParallel DML(PDML) ...

  6. android开源框架和开源项目(转)

    特效: http://www.androidviews.net/ http://www.theultimateandroidlibrary.com/ 常用效果: 1. https://github.c ...

  7. Allegro的优点与缺点

    记得刚毕业出来时就在某台商工作,用的就是allegro,从此上了贼船就下不来了--.其实还用过pcad,protel,powerpcb(以下简称3p,加上pads就4p了,呵呵--).至于mentor ...

  8. poj2578---三个数中找出第一个大于168的

    #include <stdio.h> #include <stdlib.h> int main() { int a,b,c; scanf("%d %d %d" ...

  9. 二、Cocos2dx中Android部分的c++和java实现相互调用(高级篇)

    本文由qinning199原创,转载请注明:http://www.cocos2dx.net/?p=97 本文目的 要完成在cocos2dx的场景上一个点击事件,传递一个消息到java层,下面让我们看看 ...

  10. 浅谈API设计

    为什么需要了解一些API设计? 只要你编程,你就是API Designer 一个好的设计,模块之间的耦合应该也是API级别的 一个程序,如果你独立开发,那你既是API的Designer,也是API的U ...