2

it is no problem to check if a valid WiFi connection exists. But how can I ensure only this WiFi connection is used for network access?

Assumed following scenario:

  • I check if a valid WiFi-connection exists (and may be I verify if a working Internet connection exists too)
  • now this WiFi connection is interrupted
  • I start transmitting data over network and now the mobile connection is used because WiFi died recently

How can I avoid that?

Elmi
  • 5,899
  • 15
  • 72
  • 143

3 Answers3

3

Check if WiFi connection exists:

ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (mWifi.isConnected()) {
    // Do whatever
}

Source, or use this code snippet:

private static final String DEBUG_TAG = "NetworkStatusExample";
...      
ConnectivityManager connMgr = (ConnectivityManager) 
        getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
boolean isWifiConn = networkInfo.isConnected();
networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean isMobileConn = networkInfo.isConnected();
Log.d(DEBUG_TAG, "Wifi connected: " + isWifiConn);
Log.d(DEBUG_TAG, "Mobile connected: " + isMobileConn);

Add a Listener to check if WiFi is still enabled:

public class NetworkReceiver extends BroadcastReceiver {   

@Override
public void onReceive(Context context, Intent intent) {
    ConnectivityManager conn =  (ConnectivityManager)
        context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = conn.getActiveNetworkInfo();

    // Checks the user prefs and the network connection. Based on the result, decides whether
    // to refresh the display or keep the current display.
    // If the userpref is Wi-Fi only, checks to see if the device has a Wi-Fi connection.
    if (WIFI.equals(sPref) && networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
        // If device has its Wi-Fi connection, sets refreshDisplay
        // to true. This causes the display to be refreshed when the user
        // returns to the app.
        refreshDisplay = true;
        Toast.makeText(context, R.string.wifi_connected, Toast.LENGTH_SHORT).show();

    // If the setting is ANY network and there is a network connection
    // (which by process of elimination would be mobile), sets refreshDisplay to true.
    } else if (ANY.equals(sPref) && networkInfo != null) {
        refreshDisplay = true;

    // Otherwise, the app can't download content--either because there is no network
    // connection (mobile or Wi-Fi), or because the pref setting is WIFI, and there 
    // is no Wi-Fi connection.
    // Sets refreshDisplay to false.
    } else {
        refreshDisplay = false;
        Toast.makeText(context, R.string.lost_connection, Toast.LENGTH_SHORT).show();
    }
}

Please read Managing Network Usage for a detailed solution

Community
  • 1
  • 1
ObAt
  • 2,337
  • 3
  • 24
  • 44
0

You can add broad cast receiver for network changes. So whenever wifi get interrupted you will get the notification and you can handle the situation as you wish. You can find more details in the following link http://developer.android.com/training/basics/network-ops/managing.html

android.fryo
  • 1,469
  • 1
  • 14
  • 18
0

You can check by this method that wifi is interrupted or not?

ConnectivityManager networkManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = networkManager.getActiveNetworkInfo();
NetworkInfo wifi = networkManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifi.isAvailable() && wifi.isConnected()) {
     return true;
}else {
     return false;
}

You can add broadcast receiver to lsten for network changes, you can add this method in onReceive() method, and check it, If network chanes, you will receive notification on onReceive() and then you can handle whatever you want as your wish

kumar_android
  • 2,273
  • 1
  • 21
  • 30