How to troubleshoot alerts that appear in Android 5.0: Service Intent must be explicit

Source: Internet
Author: User

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):

  1. private void Validateserviceintent (Intent service) {
  2. if (service.getcomponent () = = null && service.getpackage () = = null) {
  3. if (Getapplicationinfo (). targetsdkversion >= Build.version_codes. LOLLIPOP) {
  4. IllegalArgumentException ex = new IllegalArgumentException (
  5. "Service Intent must be explicit:" + service);
  6. Throw ex;
  7. } else {
  8. LOG.W (TAG, "implicit intents with StartService is not safe:" + Service
  9. + "" + debug.getcallers (2, 3));
  10. }
  11. }
  12. }


Since the source code is written in this way, there are two ways to solve this:

1. set action and PackageName:

The reference code is as follows:

    1. Intent mintent = new Intent ();
    2. Mintent.setaction ("XXX.XXX.XXX");//The action of the service you define
    3. Mintent.setpackage (Getpackagename ());//Here you need to set the package name of your app
    4. Context.startservice (mintent);



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.

2. Convert the implicit start to display start : --Reference address:http://stackoverflow.com/a/26318757/1446466

  1. public static Intent Getexplicitintent (context context, Intent implicitintent) {
  2. Retrieve all services that can match the given intent
  3. Packagemanager pm = Context.getpackagemanager ();
  4. list<resolveinfo> ResolveInfo = pm.queryintentservices (implicitintent, 0);
  5. Make sure-one match was found
  6. if (ResolveInfo = = NULL | | resolveinfo.size ()! = 1) {
  7. return null;
  8. }
  9. Get Component info and create ComponentName
  10. ResolveInfo serviceinfo = resolveinfo.get (0);
  11. String PackageName = serviceInfo.serviceInfo.packageName;
  12. String className = serviceInfo.serviceInfo.name;
  13. ComponentName component = new ComponentName (PackageName, className);
  14. Create a new intent. Use the old one for extras and such reuse
  15. Intent explicitintent = new Intent (implicitintent);
  16. Set the component to be explicit
  17. Explicitintent.setcomponent (component);
  18. return explicitintent;
  19. }

The method is called as follows:

    1. Intent mintent = new Intent ();
    2. Mintent.setaction ("XXX.XXX.XXX");
    3. Intent eintent = new Intent (getexplicitintent (mcontext,mintent));
    4. Context.startservice (eintent);
Copy Code

How to troubleshoot alerts that appear in Android 5.0: Service Intent must be explicit

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.