Fix the warning that appears in Android 5.0: Service Intent must be explicit

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.