Extends:http://www.eoeandroid.com/thread-568853-1-1.html
This post was last edited on 469874851 at 2015-3-11 18:15
There are times when we need to use a privacy boot for our service, but when Android 5.0 comes out, one of the features is Service Intent must be explitict , which means that service services must be started in a display mode starting with Lollipop. and the Android source code is written like this (source location: Sdk/sources/android-21/android/app/contextimpl.java):
Private voidvalidateserviceintent (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)); } } }
1,
New Intent (); Mintent.setaction ("XXX.XXX.XXX"); // you define the service's actionMintent.setpackage (Getpackagename ()); // Here you need to set your app's package name Context.startservice (mintent);
< Span style= "font-family: New song Body;" >
This is the official Google recommended workaround 。 here is the address for your reference: Http://developer.android.com/goo ... tml#billing-service, interested can go to see.
2. Convert the implicit start to display startup --reference address: http://stackoverflow.com/a/26318757/ 1446466
Public StaticIntent getexplicitintent (context context, Intent implicitintent) {//Retrieve all services that can match the given intentPackagemanager pm =Context.getpackagemanager (); List<ResolveInfo> ResolveInfo = pm.queryintentservices (implicitintent, 0); //Make sure-one match was found if(ResolveInfo = =NULL|| Resolveinfo.size ()! = 1) { return NULL; } //Get Component Info and create ComponentNameResolveInfo serviceinfo = resolveinfo.get (0); String PackageName=ServiceInfo.serviceInfo.packageName; String ClassName=ServiceInfo.serviceInfo.name; ComponentName Component=Newcomponentname (PackageName, className); //Create a new intent. use the old one for extras and such reuseIntent explicitintent =NewIntent (implicitintent); //Set the component to be explicitexplicitintent.setcomponent (component); returnexplicitintent; }
The method is called as follows:
New Intent (); Mintent.setaction ("XXX.XXX.XXX"new Intent (getexplicitintent, mintent)); Context.startservice (eintent) ;
|
Fix the warning that appears in Android 5.0: Service Intent must be explicit