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

5

15

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.

Capacitance answered 28/11, 2009 at 7:16 Comment(3)
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.Opine
indeed, so what would you suggest ?Capacitance
Possible duplicate of how to see if wifi is connected in androidCilice
B
21

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.

Blane answered 1/7, 2011 at 23:4 Comment(1)
Wouldn't it be more appropriate / correct to name this method isConnectedViaWiFi ?Ginnifer
F
8

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.

Flapjack answered 26/2, 2010 at 12:56 Comment(0)
S
5

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

Shizukoshizuoka answered 17/2, 2010 at 14:11 Comment(0)
B
2

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>
Boyse answered 17/7, 2012 at 15:17 Comment(0)
M
0

isConnected() doesnt work fully ok, research something else

final ConnectivityManager connMgr = (ConnectivityManager)
        this.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi =
    connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    final android.net.NetworkInfo mobile =
    connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if( wifi.isAvailable() && wifi.getDetailedState() == DetailedState.CONNECTED){
        Toast.makeText(this, "Wifi" , Toast.LENGTH_LONG).show();
    }
    else if( mobile.isAvailable() && mobile.getDetailedState() == DetailedState.CONNECTED ){
        Toast.makeText(this, "Mobile 3G " , Toast.LENGTH_LONG).show();
    }
    else
    {   
        Toast.makeText(this, "No Network " , Toast.LENGTH_LONG).show();
    }

this code check if you are with wifi or 3g or nothing , in the case of wifi on but not connected to a net or 3g have signal problem it detect this details, with DetailedStates

Melanochroi answered 7/12, 2012 at 18:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.