KMP KMP - 6 months ago 45
Android Question

Unable to start intent service via IMPLICIT intent


<application android:name=".MyApplication"

<service android:name=".MyService"
<action android:name="android.service.myapp.MyService.actionA"/>
<action android:name="android.service.myapp.MyService.actionB"/>
<category android:name="android.intent.category.DEFAULT"/>



If I use the following code, my service is launched:

Intent intent = new Intent(context, MyService.class);

But my service is not started if I launch it with this code:

Intent intent = new Intent("android.service.myapp.MyService.actionA");


It is insecure to use an "implicit" Intent to start or bind with a Service. Starting with Lollipop, bindService() requires an explicit Intent (your first example where you specify the Context and Class for the Service.) The behavior of startService() is undefined for implicit Intents used to start a service. From the documentation on startService():

The Intent should contain either contain the complete class name of a specific service implementation to start or a specific package name to target. If the Intent is less specified, it log a warning about this and which of the multiple matching services it finds and uses will be undefined.

If you use the explicit form, you can completely remove the <intent-filter> from the manifest: it is not needed. If you need to specify some type of work to be done by the service via the Intent, consider using a extra within the Intent.