Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANDROID: if WiFi is enabled AND active, launch an intent

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.

like image 594
Hubert Avatar asked Nov 28 '09 07:11

Hubert


4 Answers

You can use the following code to check for connectivity:

private static boolean isConnected(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager)
        context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = null;
    if (connectivityManager != null) {
        networkInfo =
            connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    }
    return networkInfo == null ? false : networkInfo.isConnected();
}

Please make sure that you've registered the android.net.conn.CONNECTIVITY_CHANGE intent in your Manifest, or else, you'll never receive a notification that you're online.

I've been struggling with this issue for the last couple of days and I just now realized that I needed to register CONNECTIVITY_CHANGE and not only WIFI_STATE_CHANGED.

like image 51
Henrique Rocha Avatar answered Oct 10 '22 09:10

Henrique Rocha


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.

like image 36
lencinhaus Avatar answered Oct 10 '22 08:10

lencinhaus


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

like image 20
Donal Rafferty Avatar answered Oct 10 '22 07:10

Donal Rafferty


In your BroadcastReceiver class:

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
    if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)){                
        boolean connected = intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);
        if (connected){
            // start your service here
        }
    }                   
}

And in your AndroidManifest.xml make sure you register for the android.net.wifi.supplicant.CONNECTION_CHANGE broadcast intent.

<intent-filter >    
    <action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
</intent-filter>
like image 20
razius Avatar answered Oct 10 '22 09:10

razius