Android Network State Check checkNetWorkStatus(Context context)

Here you can find the source of checkNetWorkStatus(Context context)

Description

check Net Work Status

Declaration

public static int checkNetWorkStatus(Context context) 

Method Source Code

//package com.java2s;

import android.content.Context;
import android.net.ConnectivityManager;

public class Main {
    public static final int WIFI_ON = 0;
    public static final int WIFI_OFF = 1;
    public static final int MOBILE_ON = 2;
    public static final int MOBILE_OFF = 3;

    public static int checkNetWorkStatus(Context context) {
        int status = 0;
        ConnectivityManager connMgr = (ConnectivityManager) context
                .getApplicationContext().getSystemService(
                        Context.CONNECTIVITY_SERVICE);
        android.net.NetworkInfo wifi = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        android.net.NetworkInfo mobile = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (wifi.isConnectedOrConnecting()) {
            status = WIFI_ON;/*from w  w  w . j a v a2s .co m*/
        } else {
            status = WIFI_OFF;
        }
        if (mobile.isConnectedOrConnecting()) {
            status = MOBILE_ON;
        } else {
            status = MOBILE_OFF;
        }

        return status;
    }
}

Related

  1. checkNetState(Context context)
  2. checkNetworkState(Context context)
  3. getMobileState(Context context)
  4. hasConnectivity(Application application)
  5. hasConnectivity(final Context context, final int... networkTypes)