BroadcastReceiver Binding Service

I have a Service class that logs several alarms.

In my BroadcastReceiver class, I want the onReceive () method to call some method of the Service class.

However, I do not see how I can bind them together. I tried to make BroadcastReceiver an inner class, but then I got more errors and could not start the alarm at all.

thanks

+7
source share
2 answers

Take a look at the http://developer.android.com/reference/android/content/BroadcastReceiver.html life cycle. BroadcastReceiver is created for message processing only. This means that life is very short, ant it is also stateless. Therefore, you cannot associate anything with it.

In any case, you can try to run the service form onReceive(Context context, Intent intent) the BroadcastReceiver method, for example:

  public void onReceive(Context context, Intent intent) { Intent intent2 = new Intent(context, GCMService.class); intent2.putExtras(intent); context.startService(intent2); } 

Then a = service should process the broadcast message.

+6
source

From http://developer.android.com/guide/components/bound-services.html

Note. Only actions, services, and content providers can bind to a service — you cannot communicate with a service from a broadcast receiver.

+2
source

All Articles