9

I have some Service class, which registers multiple alarms.

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

However, I don't see how I can bind them together. I tried to make the BroadcastReceiver an inner class, but then I got more errors and couldn't fire the alarm at all.

Thanks

mari
  • 864
  • 3
  • 12
  • 32
Mugen
  • 8,301
  • 10
  • 62
  • 140
  • I found some workaround so it's not relevant anymore. I would still like to know if this can be done. I've tried all sorts of peekSerivce() and using static instance fields, but none works. – Mugen Mar 13 '13 at 20:16

2 Answers2

6

Look at http://developer.android.com/reference/android/content/BroadcastReceiver.html life cycle. BroadcastReceiver is created only for handling a message. It means that it's life is very short, ant it is also stateless. So you cannot bind anything to it.

Anyway you can try to start a service form onReceive(Context context, Intent intent) method of BroadcastReceiver, like this:

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

Then a=the service should handle a broadcast message.

m-szalik
  • 3,546
  • 1
  • 21
  • 28
3

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

Note: Only activities, services, and content providers can bind to a service - you cannot bind to a service from a broadcast receiver.

Woland
  • 111
  • 1
  • 2
  • 9