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
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.
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(Intent.ACTION_AIRPLANE_MODE_CHANGED.equalsIgnoreCase(action)){
boolean isOn = intent.getBooleanExtra("state", false);
String serviceAction = isOn? MonitoringService.StopAction : MonitoringService.StartAction;
Intent serviceIntent = new Intent(context, MonitoringService.class);
serviceIntent.setAction(serviceAction);
context.startService(serviceIntent);
}
}
NOTE: I add following code to trigger my broadcast receiver named: ManageLocationListenerReceiver.