在Android中检查互联网连接是通过Wifi还是通过GSM/3G?

inf*_*org 2 android connectivity

我如何检查使用代码是否通过Wifi或GSM/3G在Android中进行连接?

小智 5

从这里:检测Android上的网络连接类型

// Only update if WiFi or 3G is connected and not roaming
int netType = info.getType();
int netSubtype = info.getSubtype();

if (netType == ConnectivityManager.TYPE_WIFI) {
    return info.isConnected();
 } else if (netType == ConnectivityManager.TYPE_MOBILE
        && netSubtype == TelephonyManager.NETWORK_TYPE_UMTS
        && !mTelephony.isNetworkRoaming()) {
     return info.isConnected();
 } else {
    return false;
 }
Run Code Online (Sandbox Code Playgroud)