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

时间:2010-07-16 07:30:13

标签: java android

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

4 个答案:

答案 0 :(得分:54)

我用这个:

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

您还需要

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
AndroidMainfest.xml中的

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

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

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

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

然后像这样使用它:

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

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

答案 1 :(得分:1)

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

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

之后,您可以使用以下功能检查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());
    }

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()

答案 2 :(得分:0)

首先获取对ConnectivityManager的引用,然后引用设备的check the Wifi and 3G status。 您需要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"

答案 3 :(得分:0)

您可以使用此方法检查您的互联网连接是否为 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";
    }
}

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