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):
- 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:" + service);
- Throw ex;
- } else {
- LOG.W (TAG, "implicit intents with StartService is not safe:" + Service
- + "" + debug.getcallers (2, 3));
- }
- }
- }
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:
- Intent mintent = new Intent ();
- Mintent.setaction ("XXX.XXX.XXX");//The action of the service you define
- Mintent.setpackage (Getpackagename ());//Here you need to set the package name of your app
- 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
- Public static Intent getexplicitintent (context context, Intent implicitintent) {
- //Retrieve all services that can match the given intent
- Packagemanager PM = Context.getpackagemanager ();
- list<resolveinfo> ResolveInfo = pm.queryintentservices (implicitintent, 0);
- //Make sure only one match is found
- if (ResolveInfo = = NULL | | resolveinfo.size ()! = 1) {
- return null;
- }
- //Get component info and create ComponentName
- ResolveInfo serviceinfo = resolveinfo.get (0);
- String packagename = serviceInfo.serviceInfo.packageName;
- String className = serviceInfo.serviceInfo.name;
- componentname component = new ComponentName (PackageName, className);
- //Create a new intent. use the old one for extras and such reuse
- Intent explicitintent = new Intent (implicitintent);
- //Set the component to be explicit
- explicitintent.setcomponent (component);
- return explicitintent;
- }
is to use the above code to solve the problem of the error
Copy Code The method is called as follows:
- Intent mintent = new Intent ();
- Mintent.setaction ("XXX.XXX.XXX");
- Intent eintent = new Intent (getexplicitintent (mcontext,mintent));
- 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