Reputation: 209
i am developing an android app that toggles on wifi and when it turns on performs further processing..
wifiManager.setWifiEnabled(true);
but the condition ..
wifiEnabled = wifiManager.isWifiEnabled();
if(wifiEnabled )
{ ...... }
always returns False since wifi takes time to reconnect how do i wait until it restarts or actually connects to a wifi network..
i know the condition that i can use to check if it's actually connected or connecting.
Boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
Boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
my two question?
1- wait until wifi is enabled i.e. toggled on? 2- wait untli it actually connects to a wifi network?
Upvotes: 1
Views: 664
Reputation: 11871
If you really need WIFI connection to proceed with your app logic you will have to wait. Said that, you could create an AsyncTask to handle the "waiting" situation.
That AsyncTask
would present a ProgressDialog
indicating the wifi connecting status.
If the user must toggle wifi ON manually the AsyncTask will run after wifiEnabled = true and will be stopped when manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()
== true.
I don't know if this answer helps you but this is my interpretation of your question.
Take care, hope it helps.
Upvotes: 0
Reputation: 33505
1- wait until wifi is enabled i.e. toggled on? 2- wait untli it actually connects to a wifi network?
You can for both events create BroadcastReceiver and then perform specific actions:
public class WifiStateReceiver extends BroadcastReceiver {
// action for Wi-Fi device status changes (enabled, disabled etc.)
if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
// get actual status of Wi-Fi
int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
WifiManager.WIFI_STATE_UNKNOWN);
if (state == WifiManager.WIFI_STATE_ENABLED) {
// do your stuff
}
...
// similar for WIFI_STATE_DISABLED
}
// action for connectivity changes (connected, disconnected etc.)
if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
NetworkInfo ni = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (ni.getState().equals(NetworkInfo.State.CONNECTED)) {
// do your stuff if is connected
}
...
// similar for DISCONNECTED
}
}
If you want to know more connectivity states (connecting, obtaining ip address etc.), you need to use:
networkInfo.getDetailedState();
this also depends on actual API level on device.
And finally you need these Intent-filters for BroadcastReceiver:
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<action android:name="android.net.wifi.STATE_CHANGE" />
Refs:
Upvotes: 2