1. Error description
Today in Android4.4 Xiaomi 4 mobile phone running my program without error, and on the Android 5.1 of the Huawei P7 run my program on the time of the following error, error prompts as follows:
E/androidruntime (12500): FATAL Exception:maine/androidruntime (12500): Process:com. XTC. WatchPid:12500E/androidruntime (12500): Java. Lang. IllegalArgumentException: Service Intent must be explicit:intent {act=com. XTC. Kuwo. Watch. MUSIC_play_service (has extras)}e/androidruntime (12500): at Android. App. Contextimpl. Validateserviceintent(Contextimpl. Java:1847) E/androidruntime (12500): at Android. App. Contextimpl. Startservicecommon(Contextimpl. Java:1876) E/androidruntime (12500): at Android. App. Contextimpl. StartService(Contextimpl. Java:1860) E/androidruntime (12500): at Android. Content. Contextwrapper. StartService(Contextwrapper. Java:516) E/androidruntime (12500): atcom. XTC. Watch. Kuwo. Activity. Watchmusicplay. Pausemusic(Watchmusicplay. Java:314) E/androidruntime (12500): atcom. XTC. Watch. Kuwo. Activity. Watchmusicplay. Access(Watchmusicplay. Java: +) E/androidruntime (12500): atcom. XTC. Watch. Kuwo. Activity. Watchmusicplay$. OnClick(Watchmusicplay. Java: -) E/androidruntime (12500): at Android. View. View. PerformClick(View. Java:4790) E/androidruntime (12500): at Android. View. View$PerformClick. Run(View. Java:19933) E/androidruntime (12500): at Android. OS. Handler. Handlecallback(Handler. Java:739) E/androidruntime (12500): at Android. OS. Handler. DispatchMessage(Handler. Java: the) E/androidruntime (12500): at Android. OS. Looper. Loop(Looper. Java:135) E/androidruntime (12500): at Android. App. Activitythread. Main(Activitythread. Java:5569) E/androidruntime (12500): at Java. Lang. Reflect. Method. Invoke(Native Method) E/androidruntime (12500): at Java. Lang. Reflect. Method. Invoke(Method. Java:372) E/androidruntime (12500): atcom. Android. Internal. OS. Zygoteinit$MethodAndArgsCaller. Run(Zygoteinit. Java:931) E/androidruntime (12500): atcom. Android. Internal. OS. Zygoteinit. Main(Zygoteinit. Java:726)
And the intent code that I started the service as follows:
Intent intent = new Intent(); intent.setAction(MUSIC_PLAY_SERVICE); intent.putExtra("MSG", Constants.PlayerMsg.PAUSE_MSG); //暂停播放音乐 intent.putExtra("musicURL", musicURL); //歌曲URL startService(intent);
2. Cause of Error
There are times when we need to use the service for privacy boot, but when Android 5.0 comes out, one of the features is that the service Intent must be explitict, which means that from Android Starting with the lollipop version (Android 5.0), service services must be started in the display mode.
And the Android source code is written like this (source location: Sdk/sources/android-21/android/app/contextimpl.java):
StartService (Intent Service) method
The StartService (Intent Service) method code is as follows
@Override publicstartService(Intent service) { warnIfCallingFromSystemProcess(); return startServiceCommon(service, mUser); }
Startservicecommon (Intent service, Userhandle user) method
The above StartService (Intent Service) method calls the Startservicecommon (Intent service, Userhandle user), as shown in the following code:
PrivateComponentName Startservicecommon (Intent service, Userhandle user) {try {validateserviceintent (service) ; Service.Preparetoleaveprocess (); ComponentNamecn =Activitymanagernative.Getdefault ().StartService (Mmainthread.Getapplicationthread (), service, service.Resolvetypeifneeded (Getcontentresolver ()), user.Getidentifier ());if(cn != NULL) {if(cn.Getpackagename ().equals("!")) {ThrowNewSecurityException ("not allowed to start service" +Service+ "without permission" + cn.GetClassName ()); }Else if(cn.Getpackagename ().equals("!!")) {ThrowNewSecurityException ("Unable to start service" +Service+ ": " + cn.GetClassName ()); } }return cn; } catch (RemoteException e) {return NULL; } }
Validateserviceintent (Intent Service) method
The Validateserviceintent (Intent Service) method code that is called in the above Startservicecommon (Intent service, Userhandle User) method is as follows:
Private void validateserviceintent(Intent service) {if(service.getcomponent () = =NULL&& service.getpackage () = =NULL) {if(Getapplicationinfo (). targetsdkversion >= Build.version_codes. LOLLIPOP) {IllegalArgumentException ex =NewIllegalArgumentException ("Service Intent must be explicit:"+ service);ThrowEx }Else{LOG.W (TAG,"Implicit intents with StartService is not safe:"+ Service +" "+ Debug.getcallers (2,3)); } } }
It can be seen that the validateserviceintent (Intent Service) method is judged if it is greater than build.version_codes. Lollipop version of the word, and start the service intent if not set component and package, will run out of abnormal java.lang.IllegalArgumentException: Service Intent must be explicit:
3. Workaround set the action and PackageName of the intent to start the service
Intent intent = new Intent(); intent.setAction(MUSIC_PLAY_SERVICE); intent.putExtra("MSG", Constants.PlayerMsg.PAUSE_MSG); //暂停播放音乐 intent.putExtra("musicURL", musicURL); //歌曲URL startService(intent);
Switch
Intent intent = new Intent(); intent.setAction(MUSIC_PLAY_SERVICE); 5.0以上会报:Service Intent must be explitict intent.setPackage(getPackageName()); intent.putExtra("MSG", Constants.PlayerMsg.PAUSE_MSG); //暂停播放音乐 intent.putExtra("musicURL", musicURL); //歌曲URL startService(intent);
The above code adds a line
//不加这句话的话 android 5.0以上会报:Service Intent must be explitict intent.setPackage(getPackageName());
This method is the solution that Google recommends using officially.
Attached here for your reference: Http://developer.android.com/goo Tml#billing-service, interested can go to see.
The following is the Http://developer.android.com/goo. Tml#billing-service website, as follows:
Ouyangpeng welcome reprint, sharing with people is the source of progress!
Reprint please keep the original address: Http://blog.csdn.net/ouyang_peng
My Android Advanced Tour------> How to troubleshoot warnings that appear in Android 5.0: Service Intent must be explicit: