Android : Check 3G or Wifi network is ON or Availa

2019-01-10 21:43发布

How to check that network is available or not on android device programmatically, which throws a message or toast message when we are trying to connect with a network such as Wifi & 3G.

9条回答
成全新的幸福
2楼-- · 2019-01-10 22:02

You can use this method to check whether your internet connection is 2G, 3G or 4G:

public String getNetworkClass(Context context) {
    TelephonyManager mTelephonyManager = (TelephonyManager)
            context.getSystemService(Context.TELEPHONY_SERVICE);
    int networkType = mTelephonyManager.getNetworkType();
    switch (networkType) {
        case TelephonyManager.NETWORK_TYPE_GPRS:
        case TelephonyManager.NETWORK_TYPE_EDGE:
        case TelephonyManager.NETWORK_TYPE_CDMA:
        case TelephonyManager.NETWORK_TYPE_1xRTT:
        case TelephonyManager.NETWORK_TYPE_IDEN:
            return "2G";
        case TelephonyManager.NETWORK_TYPE_UMTS:
        case TelephonyManager.NETWORK_TYPE_EVDO_0:
        case TelephonyManager.NETWORK_TYPE_EVDO_A:
        case TelephonyManager.NETWORK_TYPE_HSDPA:
        case TelephonyManager.NETWORK_TYPE_HSUPA:
        case TelephonyManager.NETWORK_TYPE_HSPA:
        case TelephonyManager.NETWORK_TYPE_EVDO_B:
        case TelephonyManager.NETWORK_TYPE_EHRPD:
        case TelephonyManager.NETWORK_TYPE_HSPAP:
            return "3G";
        case TelephonyManager.NETWORK_TYPE_LTE:
            return "4G";
        default:
            return "Unknown";
    }
}

And using following method you can check if internet is available or not, and get whether you are accessing the internet via a mobile network or WiFi:

public String getNetworkType(Context context){
    String networkType = null;
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
    if (activeNetwork != null) { // connected to the internet
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                networkType = "WiFi";
        } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
            networkType = "Mobile";
        }
    } else {
        // not connected to the internet
    }
    return networkType;
}
查看更多
我命由我不由天
3楼-- · 2019-01-10 22:03

Rahul Baradia's answer includes manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) and it's deprecated.

Below is an improved solution for the latest Android SDK.

ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
        boolean is3gEnabled = false;
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Network[] networks = connManager.getAllNetworks();
            for(Network network: networks)
            {
                NetworkInfo info = connManager.getNetworkInfo(network);
                if(info!=null) {
                    if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
                        is3gEnabled = true;
                        break;
                    }
                }
            }
        }
        else
        {
            NetworkInfo mMobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if(mMobile!=null)
                is3gEnabled = true;
        }
查看更多
放我归山
4楼-- · 2019-01-10 22:06
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

查看更多
混吃等死
5楼-- · 2019-01-10 22:08

in none of the code above i see a check for whether getNetworkInfo() returned null, which happens according to the docs when the requested network type is not supported. this suggests that on devices without 3g the app will crash with null pointer exception.

查看更多
我只想做你的唯一
6楼-- · 2019-01-10 22:09

This work for me

NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();   
String name networkInfo.getTypeName();  
查看更多
劳资没心,怎么记你
7楼-- · 2019-01-10 22:23
        // TODO Auto-generated method stub
        ConnectivityManager connMgr =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

        final android.net.NetworkInfo mobile = connectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mobile.isAvailable() && mobile.isConnected()) {
            Log.i(TAG, "Found Mobile Internet Network");
            val = true;
        }
        // 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();
        }
查看更多
登录 后发表回答