[Android L]关于Android L的Service启动问题

一 问题描述


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


  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值