I have an app some time now in which I call a service through a broadcast receiver (MyStartupIntentReceiver). The code in the broadcast receiver in order to call the service is:
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.duk3r.eortologio2.MyService");
context.startService(serviceIntent);
}
The problem is that in Android 5.0 Lollipop I get the following error (in previous versions of Android, everything works ok):
Unable to start receiver com.duk3r.eortologio2.MyStartupIntentReceiver: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.duk3r.eortologio2.MyService }
What do I have to change in order for the service to be declared as explicit and start normally? Tried some answers in other similar threads but although i got rid of the message, the service wouldn't start.
For lazy people like me just use
Send context and your intent in this method and get result intent to use.
Set your
packageName
works.Convert implicit intent to explicit intent then fire start service.
Try this. it works for me. Here MonitoringService is my service class. I have two action, which indicate service to stop or start. I send that value from my broadcast receiver depend on AIRPLANE_MODE_CHANGED.
NOTE: I add following code to trigger my broadcast receiver named: ManageLocationListenerReceiver.
I improved the answer of Shahidul to remove the context dependency:
Inside the SuperApplication class:
In your manifest:
And then, just call:
any intent you make to a service, activity etc. in your app should always follow this format
or
implicit intents (what you have in your code currently) are considered a security risk