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

Source: Internet
Author: User

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 the service Intent must be explitict, That is to say, starting with lollipop, service services must be started in a display manner.
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. }
Copy Code
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);
Copy Code

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 only one match is 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.     }

is to use the above code to solve the problem of the error

Copy Code 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
The above is the solution seen on the EoE, and at that time I was using the Aidl service, tested two ways, the first to add setpackage this code after the aidl of the service to prompt the binding failure, the second way to resolve the occurrence of the exception problem, If there is a better solution, I would like to leave a message or private messages, in order to learn Knowledge Update blog

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

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.