一 问题描写叙述

Android L[Android5.X.X] 版本号通过Intent隐式启动service时将会报出下面错误:

AndroidRuntime(  792):
java.lang.IllegalArgumentException: Service Intent must be explicit

【声明】欢迎转载,但请保留文章原始出处:http://blog.csdn.net/yelangjueqi/article/details/46754581



具体信息:

01-02 07:52:44.736 D/PowerManagerService/SmartStandby(  792): sendDetectFaceIntent:com.wtk.smart.standby.DETECT_FACE_ACTION

01-02 07:52:44.738 W/ContextImpl(  792): Calling a method in the system process without a qualified user: android.app.ContextImpl.startService:1813 com.android.server.power.PowerManagerService.sendDetectFaceIntent:4155 com.android.server.power.PowerManagerService.handleDetectFaceCase:4137
com.android.server.power.PowerManagerService.access$4400:100 com.android.server.power.PowerManagerService$PowerManagerHandler.handleMessage:3306

01-02 07:52:44.744 E/AndroidRuntime(  792): *** FATAL EXCEPTION IN SYSTEM PROCESS: PowerManagerService

01-02 07:52:44.744 E/AndroidRuntime(  792): java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.wtk.smart.standby.DETECT_FACE_ACTION (has extras) }

01-02 07:52:44.744 E/AndroidRuntime(  792):  at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1801)

01-02 07:52:44.744 E/AndroidRuntime(  792):  at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1830)

01-02 07:52:44.744 E/AndroidRuntime(  792):  at android.app.ContextImpl.startService(ContextImpl.java:1814)

01-02 07:52:44.744 E/AndroidRuntime(  792):  at com.android.server.power.PowerManagerService.sendDetectFaceIntent(PowerManagerService.java:4155)

01-02 07:52:44.744 E/AndroidRuntime(  792):  at com.android.server.power.PowerManagerService.handleDetectFaceCase(PowerManagerService.java:4137)

01-02 07:52:44.744 E/AndroidRuntime(  792):  at com.android.server.power.PowerManagerService.access$4400(PowerManagerService.java:100)

01-02 07:52:44.744 E/AndroidRuntime(  792):  at com.android.server.power.PowerManagerService$PowerManagerHandler.handleMessage(PowerManagerService.java:3306)

01-02 07:52:44.744 E/AndroidRuntime(  792):  at android.os.Handler.dispatchMessage(Handler.java:111)

01-02 07:52:44.744 E/AndroidRuntime(  792):  at android.os.Looper.loop(Looper.java:194)

01-02 07:52:44.744 E/AndroidRuntime(  792):  at android.os.HandlerThread.run(HandlerThread.java:61)

01-02 07:52:44.744 E/AndroidRuntime(  792):  at com.android.server.ServiceThread.run(ServiceThread.java:46)

01-02 07:52:44.752 V/SettingsProvider(  792): call(global:dropbox:system_server_crash) for 0

01-02 07:52:44.753 D/SettingsProvider(  792): lookupValue table global cache.fullyMatchesDisk() dropbox:system_server_crash

01-02 07:52:44.757 V/SettingsProvider(  792): call(global:logcat_for_system_server_crash) for 0

01-02 07:52:44.757 D/SettingsProvider(  792): lookupValue table global cache.fullyMatchesDisk() logcat_for_system_server_crash

二 问题分析

A . 定位问题点

sdk\sources\android-21\android\app\ContextImpl.java

class ContextImpl extends Context {

 ......

    private void validateServiceIntent(Intent service) {

        if (service.getComponent() == null && service.getPackage() == null) {

            if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {

                IllegalArgumentException ex = new IllegalArgumentException(

                        "Service Intent must be explicit: " + service);

                throw ex;

            } else {

                Log.w(TAG, "Implicit intents with startService are not safe: " + service

                        + " " + Debug.getCallers(2, 3));

            }

        }

    }

 ......

}

B .分析过程

上面源代码中蓝色加粗部分:service.getComponent() == null && service.getPackage() == null

表明通过intent启动service时, 须要指定Intent的ComponentName信息:intent.setComponent(xxx),或指定Intent的setPackage("包名"),假设两者都没有指定的话将会报以上错误。尤其在framework层启动APP层的service时。假设是隐式启动service,可能会导致系统进程挂掉。出现不断重新启动的现象。

三 解决方法

參考一

Intent intent = new Intent();

    ComponentName componentName = new ComponentName(pkgName,serviceName);

    intent.setComponent(componentName);

    context.startService(intent);

參考二

Intent mIntent = new Intent();

mIntent.setAction("XXX.XXX.XXX");//Service可以匹配的Action

mIntent.setPackage(pkgName);//应用的包名

context.startService(mIntent);

四 延伸官网

Binding to a Service

The Context.bindService() method now requires an explicit Intent, and throws an exception if given an implicit intent. To ensure your app is secure, use an explicit intent when starting or binding your Service, and do not declare intent filters for the service.

也就是说。在5.0以后不同意使用隐式Intent方式来启动Service

[Android L]关于Android L的Service启动问题的更多相关文章

  1. android中service启动后台程序

    Service是Android中一个类,它是Android四大组件之一,使用Service可以在后台执行长时间的操作( perform long-running operations in the b ...

  2. 转Android service 启动篇之 startForegroundService

    本文转自:https://blog.csdn.net/shift_wwx/article/details/82496447 前言: 在官方文档 Android 8.0 行为变更 中有这样一段话: An ...

  3. Android Service 启动和停止服务

    activity_main.xml 定义两个Button控件,start_service和stop_service. <LinearLayout xmlns:android="http ...

  4. Android 四大组件之再论service

    service常见的有2种方式,本地service以及remote service. 这2种的生命周期,同activity的通信方式等,都不相同. 关于这2种service如何使用,这里不做介绍,只是 ...

  5. Android 综合揭秘 —— 全面剖释 Service 服务

    引言 Service 服务是 Android 系统最常用的四大部件之一,Android 支持 Service 服务的原因主要目的有两个,一是简化后台任务的实现,二是实现在同一台设备当中跨进程的远程信息 ...

  6. Android组件系列----Android Service组件深入解析

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  7. Android基调(十六)- Service:startService()、stopService()、bindService()、unbindService()加

    直行 第一 另外一种 第三种 总结 开门见山 开启服务有三种情况:假设直接使用服务,则没有必要进行绑定,可是假设要使用服务里面的方法.则要进行绑定. 具体的启动情况有下: ①调用startServic ...

  8. Android Binder分析二:Natvie Service的注冊

    这一章我们通过MediaPlayerService的注冊来说明怎样在Native层通过binder向ServiceManager注冊一个service,以及client怎样通过binder向Servi ...

  9. Android中startService的使用及Service生命周期

    Android中有两种主要方式使用Service,通过调用Context的startService方法或调用Context的bindService方法.本文仅仅探讨纯startService的使用.不 ...

  10. Android 7.1.1 锁屏界面启动流程

    前几天遇到一个低概率复现锁屏界面不显示,仅仅显示状态栏的问题,跟了下锁屏界面启动显示的流程,在这分享下,也方便以后自己查看.前面简介了下Zygote启动流程, Zygote进程启动后会首先创建一个Sy ...

随机推荐

  1. Array.apply 方法的使用

    Array.apply(null, {length: 5}) length为特殊字段,意思是生成一个长度为5的数组,由于没赋值,所以都是undefined; 如果要赋值,可以这样 console.lo ...

  2. JS中event.preventDefault()取消默认事件能否还原?

    参考知乎讨论:https://www.zhihu.com/question/21891734

  3. http://blog.csdn.net/rosten/article/details/17068285

    http://blog.csdn.net/rosten/article/details/17068285

  4. python 列表合并

    列表合并主要有以下方法: 1.用list的extend方法,L1.extend(L2),该方法将参数L2的全部元素添加到L1的尾部 结果:[1, 2, 3, 4, 5, 1, 20, 30] 2.用切 ...

  5. html DOM 的继承关系

    零散的知识聚合在一起,就会形成力量,就有了生命力. 如各种语言的开发框架, 都是右各个碎片化的功能聚合在一起,构成有机地整体,便有了强大的力量.will be powerful! 如: jquery ...

  6. hibernate学习系列-----(9)hibernate对集合属性的操作之Map集合篇

    照旧,先新建一个StudentMap.java实体类,将hobby属性使用map集合接口来存放: package com.joe.entity; import java.util.Map; publi ...

  7. 【android相关】【问题解决】R.java文件丢失

    在进行android开发过程中,有时候,我们会遇到gen文件中R.java丢失的现象.重新build,或者clean工程,close并重新打开Project,但有时也没解决. 这可能是由于不小心把xm ...

  8. CSS/JavaScript hacks,browserhacks使用

    1.网址 http://browserhacks.com/ 2.使用 (1)JavaScript Hacks 浏览器js判断 (2)条件注释hack (3)Media Query Hacks 媒体查询 ...

  9. 解决Odoo访问fonts.googleapis.com导致速度慢的问题

    Odoo中有些css文件引用了谷歌字体,但因为谷歌服务器被墙,导致部分页面加载受影响. 解决方法如下: 360网站卫士常用前端公共库CDN服务把谷歌字体库都存到它的CDN上了,因此我们只需把样式表中谷 ...

  10. 理解shell中的atime,mtime,ctime

    所有文件都有3个时间信息,保存在文件系统中 atime (Access time)是文件最后一此读的时间 或者执行文件的时间 mtime (Modified time)是文件最后一次写的时间(是在写入 ...