"Perfect Solution Series" Android5.0 above implicit intents with StartService is not safe

Source: Internet
Author: User

On Android6.0, the following code is used:

Intent Intent = new Intent () intent.setaction ("Xxx.server"); Bindservice (Intent, Mconn, context.bind_auto_create);

The exception that prompted the warning:

Implicit intents with StartService is not safe

Check the source code, the original 5.0 must be forced to use the display mode to start the service.

private void Validateserviceintent (Intent service) {        if (service.getcomponent () = = NULL && Service.getpackage () = null) {            if (Getapplicationinfo (). targetsdkversion >= Build.version_codes. LOLLIPOP) {                illegalargumentexception ex = new IllegalArgumentException (                        "Service Intent must be explicit:" + Serv ICE);                Throw ex;            } else {                LOG.W (TAG, "implicit intents with StartService is not safe:" + Service                        + "" + debug.getcallers (2, 3)); 
   }}}    
Workaround:

When you set the action, you also need to set the PackageName to start or bind the service class.
We use the system's Getpackagename function here to get the package name.

Intent Intent = new Intent () intent.setaction ("Xxx.server"); Intent.setpackage (Context.getpackagename ()); Bindservice (Intent, Mconn, context.bind_auto_create);

  

Perfect Solution Series Android5.0 above implicit intents with StartService is not safe

Related Article

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.