Java 如何检查 wifi 或 3g 网络在 android 设备上可用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3262781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:50:28  来源:igfitidea点击:

how to check wifi or 3g network is available on android device

javaandroid

提问by Govardhan Reddy

Here, my android device supports both wifi and 3g. At particular time which network is available on this device. Because my requirement is when 3g is available I have to upload small amount of data. when wifi is available entire data have to upload. So, I have to check connection is wifi or 3g. Please help me. Thanks in advance.

在这里,我的 android 设备同时支持 wifi 和 3g。在特定时间,此设备上可用的网络。因为我的要求是当 3g 可用时,我必须上传少量数据。当wifi可用时,必须上传整个数据。所以,我必须检查连接是wifi还是3g。请帮我。提前致谢。

回答by Pentium10

I use this:

我用这个:

/**
 * Checks if we have a valid Internet Connection on the device.
 * @param ctx
 * @return True if device has internet
 *
 * Code from: http://www.androidsnippets.org/snippets/131/
 */
public static boolean haveInternet(Context ctx) {

    NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx
            .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

    if (info == null || !info.isConnected()) {
        return false;
    }
    if (info.isRoaming()) {
        // here is the roaming option you can change it if you want to
        // disable internet while roaming, just return false
        return false;
    }
    return true;
}

You also need

你还需要

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

in AndroidMainfest.xml

在 AndroidMainfest.xml 中

To get the network type you can use this code snippet:

要获取网络类型,您可以使用以下代码片段:

ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

//mobile
State mobile = conMan.getNetworkInfo(0).getState();

//wifi
State wifi = conMan.getNetworkInfo(1).getState();

and then use it like that:

然后像这样使用它:

if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
    //mobile
} else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
    //wifi
}

To get the type of the mobile network I would try TelephonyManager#getNetworkTypeor NetworkInfo#getSubtypeName

要获取移动网络的类型,我会尝试TelephonyManager#getNetworkTypeNetworkInfo#getSubtypeName

回答by Akhil

You need to add below permission in android manifest file:

您需要在android清单文件中添加以下权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

After that you can use below functions to check wifi or mobile network is connected or not

之后,您可以使用以下功能检查wifi或移动网络是否已连接

public static boolean isWifiConnected(Context context) {
        ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        return ((netInfo != null) && netInfo.isConnected());
    }

public static boolean isMobileConnected(Context context) {
        ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        return ((netInfo != null) && netInfo.isConnected());
    }

Some references from developer.android.com are:

developer.android.com 的一些参考资料是:

  1. https://developer.android.com/reference/android/net/ConnectivityManager.html
  2. https://developer.android.com/reference/android/net/NetworkInfo.html
  3. https://developer.android.com/reference/android/net/ConnectivityManager.html#getActiveNetworkInfo()
  1. https://developer.android.com/reference/android/net/ConnectivityManager.html
  2. https://developer.android.com/reference/android/net/NetworkInfo.html
  3. https://developer.android.com/reference/android/net/ConnectivityManager.html#getActiveNetworkInfo()

回答by Salmaan

First get a reference to the ConnectivityManager and then check the Wifi and 3G statusof the device. You'll need the ACCESS_NETWORK_STATE permission to use this service.

首先获取对 ConnectivityManager 的引用,然后检查设备的 Wifi 和 3G 状态。您需要 ACCESS_NETWORK_STATE 权限才能使用此服务。

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

    if (mWifi.isAvailable() == true) {
        return "Connected to WiFi";
    } else if (mMobile.isAvailable() == true) {
        return "Connected to Mobile Network";
    } else return "No internet Connection"

回答by zeeali

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

您可以使用此方法来检查您的互联网连接是2G、3G 还是 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:

并使用以下方法检查互联网是否可用,并了解您是否通过移动网络或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;
}