有些时候我们使用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 装饰器、内部函数、闭包简单理解

    python内部函数.闭包共同之处在于都是以函数作为参数传递到函数,不同之处在于返回与调用有所区别. 1.python内部函数 python内部函数示例: def test(*args): def a ...

  2. vb socket的使用

    说明:原本在 csdn 博客 写博客的,因为使用的移动宽带,csdn的 博客无法访问,所以先暂时到博客园写博客了 有能解决移动宽带 有部分网站不能访问的问题,请联系我,QQ 809775607 /** ...

  3. sqlite3编译与查询

    1.sqlite3 http://www.sqlite.org/ 下载 wget http://www.sqlite.org/2014/sqlite-amalgamation-3080403.zip ...

  4. Others in life

    耗电量主要是与电机有关,800W电机在48V下的工作电流大约是800/48=16.7A,因此其工作时间主要取决于电池的容量,如果电池容量是20Ah,那么大概也就连续工作1个小时左右,也就是30-40k ...

  5. stormzhang的推荐!

    欢迎转载,但请务必在明确位置注明出处!http://stormzhang.com/android/2014/07/07/learn-android-from-rookie/ QQ交流群:入群理由请正确 ...

  6. Debug程序无法运行解决

    说明:文章内容部分参考网络上的解决思路. 在没有安装Microsoft Visual Studio的系统上,Debug版本无法正常运行.这是由于缺少vs运行时库引起的. 以vs2005为例.开发机用v ...

  7. poj2070简单题

    #include <stdio.h> #include <stdlib.h> int main() { float sped; int wei,sth; while(scanf ...

  8. 使用 Eclipse Memory Analyzer 进行堆转储文件分析

    Eclipse Memory Analyzer(MAT)是著名的跨平台集成开发环境 Eclipse Galileo 版本的 33 个组成项目中之一,它是一个功能丰富的 JAVA 堆转储文件分析工具,可 ...

  9. servlet生成随机验证码

    package com.cgyue; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import jav ...

  10. mywebsite1

    http://www.wuweierwei.com/ 个人网站 http://www.yyyweb.com/demo/icons-filling/index.html CSS3图标填充效果 http: ...