java.lang.IllegalArgumentException: Service Intent must be explicit

       最近开发的一款应用在Android 5.0 手机上出现了崩溃现象,log信息如下:

05-14 12:48:36.363 11954 11954 E AndroidRuntime: java.lang.RuntimeException: Unable to start receiver com.yulore.recognize.android.receiver.PhoneStateReceiver: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.yulore.recognize.android (has extras) }
05-14 12:48:36.363 11954 11954 E AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:2587)
05-14 12:48:36.363 11954 11954 E AndroidRuntime: at android.app.ActivityThread.access$1700(ActivityThread.java:147)
05-14 12:48:36.363 11954 11954 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358)
05-14 12:48:36.363 11954 11954 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
05-14 12:48:36.363 11954 11954 E AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
05-14 12:48:36.363 11954 11954 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5232)
05-14 12:48:36.363 11954 11954 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
05-14 12:48:36.363 11954 11954 E AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
05-14 12:48:36.363 11954 11954 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
05-14 12:48:36.363 11954 11954 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
05-14 12:48:36.363 11954 11954 E AndroidRuntime: Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.yulore.recognize.android (has extras) }
05-14 12:48:36.363 11954 11954 E AndroidRuntime: at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1676)
05-14 12:48:36.363 11954 11954 E AndroidRuntime: at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1705)
05-14 12:48:36.363 11954 11954 E AndroidRuntime: at android.app.ContextImpl.startService(ContextImpl.java:1689)
05-14 12:48:36.363 11954 11954 E AndroidRuntime: at android.content.ContextWrapper.startService(ContextWrapper.java:515)
05-14 12:48:36.363 11954 11954 E AndroidRuntime: at com.yulore.recognize.android.receiver.PhoneStateReceiver.startService(PhoneStateReceiver.java:85)
05-14 12:48:36.363 11954 11954 E AndroidRuntime: at com.yulore.recognize.android.receiver.PhoneStateReceiver.onReceive(PhoneStateReceiver.java:53)
05-14 12:48:36.363 11954 11954 E AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:2580)
05-14 12:48:36.363 11954 11954 E AndroidRuntime: ... 9 more


原因是Android 5.0对Service Intent的调用策略发生改变了,必须通过显示Intent来启动Service,详细介绍可以看stackoverflow上的介绍。


解决办法如下:

1、通过显示意图启动Service(直接用类名);

Intent intent = new Intent(com.yulore.test.AppService.class);
context.startService(intent);


2、如果想继续使用隐式意图的话,加上包名信息即可;

Intent intent = new Intent();
intent.setAction("com.yulore.recognize.android");
intent.setPackage(context.getPackageName());	//兼容Android 5.0
context.startService(intent);






你可能感兴趣的:(Android,Service,Intent,explicit)