views:

1532

answers:

4

This is what I would like to do :

=> IF WiFi is enabled AND active, launch an intent (in fact it's a WebView that gets its content=>the instructions of my app on the web)

=> IF NOT, then I would launch another intent so that I don't show a WebView with "Web page not available ... The Web page at http://www.mywebsite.com might be temporarily down or it may have moved ..."

I tought initially to use

if(wifi.isWifiEnabled())

but that does not say if the Wifi connection is ACTIVE or not. It says only that the user has turned the switch on. The device may or may not be connected... Is this correct ?

Then I tried to use :

if (wifi.getConnectionInfo().getSSID()!= null)

but I noticed that it returns a string even if the connection has been lost or has been disabled ... ?

How should I do then ?

wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
Intent intent_instructions;

         if (wifi.getConnectionInfo().getSSID()!= null){
          Log.i("Hub", "WiFi is enabled AND active !");
          Log.i("Hub", "SSID = "+wifi.getConnectionInfo().getSSID());
          intent_instructions = new Intent(this, Instructions.class);
      }else{
       Log.i("Hub", "NO WiFi");
       intent_instructions = new Intent(this, Instructions_No_WiFi.class);
      }
      this.startActivity(intent_instructions);

Is there a more general way to test if the device has the connectivity to the internet just before launching an intent ? be it through Wifi, 3G, etc ...

Thanks in advance for your help.

+1  A: 

Try getWiFiState(), checking for WIFI_STATE_ENABLED.

CommonsWare
as I said in the question : WIFI_STATE_ENABLED does not say if the Wifi connection is ACTIVE or not. It says only that the user has turned the switch on. The device may or may not be connected... depending if there's a hotspot around, isn't it ?
Hubert
+1  A: 

WIFI_STAT_ENABLED just means the WIFI radio is on. This doesn't have anything to do with whether or not you are actually connected to an access point.

Chris
indeed, so what would you suggest ?
Hubert
+3  A: 

You can do it as follows:

  @Override
  public void onReceive(Context context, Intent intent) {

  String action = intent.getAction();

        if(WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)){

         Log.d("WIFI", "WIFI has changed");
         int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, -1);
         Log.d("WIFI", "WIFI State = " + wifiState);
         setCurrentWifiState(wifiState);

         }  

You will get 0,1,2,3 depending on which state the Wifi is in, so for example 2 is connecting, you can check the rest in the documents

Donal Rafferty
+4  A: 

Try android.net.ConnectivityManager.getActiveNetworkInfo(): if it returns null you have no connection; if it returns a NetworkInfo object, you can check the connection's state with NetworkInfo.getState(), and if it's NetworkInfo.State.Connected then you're connected, else you're not.

lencinhaus
will try that thanks. H.
Hubert
indeed that works great, thank you.
Hubert