问题现象:
在Android 5.0采用隐式意图启动方式,如下:
Intent intent = new Intent("com.haier.oet.test");
bindService(intent, conn, Context.BIND_AUTO_CREATE);
运行后,产生了如下所示的bug:
04-27 12:09:49.033 718-718/com.haier.oet.androidplayerground E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.haier.oet.androidplayerground, PID: 718
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4221)
at android.view.View.performClick(View.java:5155)
at android.view.View$PerformClick.run(View.java:20747)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4216)
at android.view.View.performClick(View.java:5155)
at android.view.View$PerformClick.run(View.java:20747)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.haier.oet.test }
at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:2027)
at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:2135)
at android.app.ContextImpl.bindService(ContextImpl.java:2113)
at android.content.ContextWrapper.bindService(ContextWrapper.java:559)
at com.haier.oet.androidplayerground.MainActivity3.buttonClick(MainActivity3.java:60)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4216)
at android.view.View.performClick(View.java:5155)
at android.view.View$PerformClick.run(View.java:20747)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
问题原因:
有些时候我们使用Service的时需要采用隐式意图启动的方式,但是Android 5.0一出来后,其中有个特性就是Service Intent must be explitict,也就是说从Lollipop开始,Service服务必须采用显式意图方式启动。
解决方法:
Intent intent = new Intent();
intent.setAction("com.haier.oet.test");
intent.setPackage("com.haier.oet.androidplayerground"); //指定启动的是那个应用(com.haier.oet.androidplayerground)中的Action(com.haier.oet.test)指向的服务组件