-5

Basically I want to broadcast and receive every 5 seconds using this code:

    private final BroadcastReceiver checkWifiStateChanged = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {    
            checkWifiStateHasChanged();

        }
    };

How could I do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ryan Ciehanski
  • 321
  • 3
  • 6
  • 16
  • There's probably a better solution. What are you trying to accomplish? – Programmer Bruce Jun 05 '11 at 03:57
  • I want to check if the wifi has been turned off or on from an outside source like every second then setChecked a Togglebutton – Ryan Ciehanski Jun 05 '11 at 03:58
  • 1
    You only need to register the receiver for the events you're interested in, and the Android platform will ensure your receiver is notified in a timely manner when the events occur. You'll have to accept that it won't necessarily be within 1 second or even within 5 seconds. Are you looking for help with how to register a receiver for the events you're interested in? – Programmer Bruce Jun 05 '11 at 04:01
  • Yes if you could help me when the wifi is turned off or on I want this BR to be triggered to check the togglebutton on or off – Ryan Ciehanski Jun 05 '11 at 04:05
  • 1
    The www is rich with examples of this. Just yesterday someone helped you with how to register a receiver. http://stackoverflow.com/questions/6232600/how-to-use-broadcast-and-receivers What have you tried? For the specific broadcasts you're probably interested in, take a look at http://developer.android.com/reference/android/net/wifi/WifiManager.html. – Programmer Bruce Jun 05 '11 at 04:12
  • Someone voted my question down :S – Ryan Ciehanski Jun 05 '11 at 05:10

1 Answers1

2

As the comments have stated, this is basically an unnecessary way to approach this. The Android platform automatically sends out Intents for certain changes, and Wifi status is one of them. You need only have a BroadcastReceiver registered that filters on that Intent to be able to respond to it.

Maximus
  • 8,351
  • 3
  • 29
  • 37
  • what is the intent/filter for wifi? – Ryan Ciehanski Jun 05 '11 at 04:06
  • 1
    Dug around a few minutes and it should be the action "android.net.wifi.WIFI_STATE_CHANGED". Have a look at this post as well http://stackoverflow.com/questions/1811852/android-if-wifi-is-enabled-and-active-launch-an-intent – Maximus Jun 05 '11 at 04:13