Starting Service from BroadcastReceiver

AndroidBroadcastreceiver

Android Problem Overview


I have a Service and BroadcastReceiver in my application, but how do I launch the service directly from the BroadcastReceiver? Using

startService(new Intent(this, MyService.class));

does not work in a BroadcastReceiver, any ideas?

EDIT:

> context.startService(..);

works, I forgot the context part

Android Solutions


Solution 1 - Android

Don't forget

> context.startService(..);

Solution 2 - Android

should be like that:

Intent i = new Intent(context, YourServiceName.class);
context.startService(i);

be sure to add the service to manifest.xml

Solution 3 - Android

use the context from the onReceive method of your BroadcastReceiver to start your service component.

@Override
public void onReceive(Context context, Intent intent) {
      Intent serviceIntent = new Intent(context, YourService.class);
      context.startService(serviceIntent);
}

Solution 4 - Android

Best Practice :

While creating an intent especially while starting from BroadcastReceiver, dont take this as context. Take context.getApplicationContext() like below

 Intent intent = new Intent(context.getApplicationContext(), classNAME);
context.getApplicationContext().startService(intent);

Solution 5 - Android

 try {
        Intent intentService = new Intent(context, MyNewIntentService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(intentService );
        } else {
            context.startService(intentService );
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

Solution 6 - Android

> Because a receiver's onReceive(Context, Intent) method runs on the main thread, it should execute and return quickly. If you need to perform long running work, be careful about spawning threads or starting background services because the system can kill the entire process after onReceive() returns. For more information, see Effect on process state To perform long running work, we recommend:

> Calling goAsync() in your receiver's onReceive() method and passing the BroadcastReceiver.PendingResult to a background thread. This keeps the broadcast active after returning from onReceive(). However, even with this approach the system expects you to finish with the broadcast very quickly (under 10 seconds). It does allow you to move work to another thread to avoid glitching the main thread. Scheduling a job with the JobScheduler developer.android.com

Solution 7 - Android

It's better to use ContextCompat:

Intent serviceIntent = new Intent(context, ForegroundService.class);
ContextCompat.startForegroundService(context, serviceIntent);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSamuelView Question on Stackoverflow
Solution 1 - AndroidSamuelView Answer on Stackoverflow
Solution 2 - AndroidZeev GView Answer on Stackoverflow
Solution 3 - AndroidSachin K PissayView Answer on Stackoverflow
Solution 4 - AndroidARUNView Answer on Stackoverflow
Solution 5 - AndroidPeterView Answer on Stackoverflow
Solution 6 - AndroidHojjat ZohooriView Answer on Stackoverflow
Solution 7 - AndroidSabrinaView Answer on Stackoverflow