简体   繁体   English

Android设备上如何检查wifi或3g网络

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

Here, my android device supports both wifi and 3g. 在这里,我的Android设备支持wifi和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. 因为我的要求是3g可用时我必须上传少量数据。 when wifi is available entire data have to upload. 当wifi可用时,必须上传整个数据。 So, I have to check connection is wifi or 3g. 所以,我要检查连接是wifi还是3g。 Please help me. 请帮我。 Thanks in advance. 提前致谢。

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#getNetworkType or NetworkInfo#getSubtypeName 要获得移动网络的类型,我会尝试TelephonyManager#getNetworkTypeNetworkInfo#getSubtypeName

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 https://developer.android.com/reference/android/net/ConnectivityManager.html
  2. https://developer.android.com/reference/android/net/NetworkInfo.html https://developer.android.com/reference/android/net/NetworkInfo.html
  3. https://developer.android.com/reference/android/net/ConnectivityManager.html#getActiveNetworkInfo() https://developer.android.com/reference/android/net/ConnectivityManager.html#getActiveNetworkInfo()

First get a reference to the ConnectivityManager and then check the Wifi and 3G status of the device. 首先获取ConnectivityManager的参考,然后检查设备的Wifi和3G状态 You'll need the ACCESS_NETWORK_STATE permission to use this service. 您需要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"

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM