views:

600

answers:

2

Hi all,

I want to send an intent to my service everytime the state of Wifi connectivity changes.

So when I currently use a broadcast receiver to listen for the state changes in Wifi, so when this recieves an intent I want to be able to send this info on to my service.

Is this possible and if so the correct way to do it?

+2  A: 

If the service is going to be running at the time, you could just register a BroadcastReceiver in the Service directly via registerReceiver().

Otherwise, call startService() from the BroadcastReceiver to let the Service know of the event, starting up the Service if it is not running. Be sure to shut down that Service at some point (e.g., use IntentService, which will automatically shut itself down when there is no more work to do).

CommonsWare
Thanks, the service is a Sip Service that will be running the whole time periodically registering with a sip registry server. I have a seperate wifi class that has the broadcast receivers for listening out for RSSI level changes and Wifi connectivity changes. I would like to keep that seperate to the Sip service and just tell the Sip service when Wifi has lost connection to stop trying to register and when it gets connection try to register
Donal Rafferty
Use the first pattern -- register a `BroadcastReceiver` for the relevant broadcasts in the `Service` itself via `registerReceiver()`. The "keep that separate" is a coding issue and should not dictate your Android architecture.
CommonsWare
So you recommend taking the broadcast receivers out of my Wifi class and putting them into the Service? So I want to have the Wifi class listen for the changes and then let several different Services and activies know about the change so I would have thought having the Wifi class listen for the changes in one spot would be better than loads of broadcast receivers in different Services and Activities? So I was thinking of using Custom Intents?
Donal Rafferty
You keep changing your story, which makes it very exasperating to try to assist you. Unless your "Wifi class" will do hundreds or thousands of lines of business logic, each component that cares about WiFi state changes may as well have their own listener for them. After all, they're going to need their own listener for your "Custom Intents" -- all your "Wifi class" is doing is adding overhead. If the "Wifi class" will be doing a lot of work, perhaps that overhead is warranted, but I have no way to tell. But don't have a component receive intents just to broadcast new ones without more reason.
CommonsWare
Thanks, I apologise for not being clear, your knowledge is greatly appreciated
Donal Rafferty
And I apologize for my tone.
CommonsWare
A: 

How do you listen to Wifi state changes?

I've been trying to find something like onStateChangedListener,but no luck.

tbilisi