Java Code Examples for android.net.NetworkInfo#getExtraInfo()

The following examples show how to use android.net.NetworkInfo#getExtraInfo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Tools.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
/**
 * 获取网络类型
 * 
 * @return
 */
public static String getNetworkType(Context context) {

	ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	if (manager == null) {
		return "";
	}
	NetworkInfo networkInfo = manager.getActiveNetworkInfo();
	if (networkInfo != null && networkInfo.isConnected()) {
		if (ConnectivityManager.TYPE_WIFI == networkInfo.getType()) {
			return networkInfo.getTypeName();
		}
		return networkInfo.getExtraInfo();
	}

	return "network unavailable";
}
 
Example 2
Source File: CommonUtils.java    From sealtalk-android with MIT License 6 votes vote down vote up
/**
 * 获取当前网络类型
 * 
 * @return 0:没有网络 1:WIFI网络 2:WAP网络 3:NET网络
 */
public static int getNetworkType(Context context) {
	int netType = 0;
	ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
	if (networkInfo == null) {
		return netType;
	}
	int nType = networkInfo.getType();
	if (nType == ConnectivityManager.TYPE_MOBILE) {
		String extraInfo = networkInfo.getExtraInfo();
		if (!TextUtils.isEmpty(extraInfo)) {
			if (extraInfo.toLowerCase(Locale.getDefault()).equals("cmnet")) {
				netType = NETTYPE_CMNET;
			} else {
				netType = NETTYPE_CMWAP;
			}
		}
	} else if (nType == ConnectivityManager.TYPE_WIFI) {
		netType = NETTYPE_WIFI;
	}
	return netType;
}
 
Example 3
Source File: Network.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
public static String getActiveConnPoint(Context context)
{
    if (isWIFIConnected(context))
    {
        return "wifi";
    }
    ConnectivityManager connectivitymanager = (ConnectivityManager)context.getSystemService("connectivity");
    if (connectivitymanager == null)
    {
        return "";
    }
    NetworkInfo networkinfo = connectivitymanager.getActiveNetworkInfo();
    if (networkinfo == null)
    {
        return "";
    } else
    {
        return networkinfo.getExtraInfo();
    }
}
 
Example 4
Source File: IOTWifiModule.java    From react-native-iot-wifi with MIT License 6 votes vote down vote up
private String getWifiSSID() {
    WifiInfo info = wifiManager.getConnectionInfo();
    String ssid = info.getSSID();

    if (ssid == null || ssid.equalsIgnoreCase("<unknown ssid>")) {
        NetworkInfo nInfo = connectivityManager.getActiveNetworkInfo();
        if (nInfo != null && nInfo.isConnected()) {
            ssid = nInfo.getExtraInfo();
        }
    }

    if (ssid != null && ssid.startsWith("\"") && ssid.endsWith("\"")) {
        ssid = ssid.substring(1, ssid.length() - 1);
    }

    return ssid;
}
 
Example 5
Source File: CommonUtils.java    From lunzi with Apache License 2.0 6 votes vote down vote up
/**
 * 获取当前网络类型
 *
 * @param context
 * @return 0:没有网络 1:WIFI网络 2:WAP网络 3:NET网络
 */
public static int getNetworkType(Context context) {
    int netType = 0;
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo == null) {
        return netType;
    }
    int nType = networkInfo.getType();
    if (nType == ConnectivityManager.TYPE_MOBILE) {
        String extraInfo = networkInfo.getExtraInfo();
        if (!TextUtils.isEmpty(extraInfo)) {
            if (extraInfo.toLowerCase().equals("cmnet")) {
                netType = NETTYPE_CMNET;
            } else {
                netType = NETTYPE_CMWAP;
            }
        }
    } else if (nType == ConnectivityManager.TYPE_WIFI) {
        netType = NETTYPE_WIFI;
    }
    return netType;
}
 
Example 6
Source File: StateMachineConnection.java    From timelapse-sony with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void process(final StateMachineConnection sm) {

    // Workaround when there is a data connection more than the wifi one
    // http://stackoverflow.com/questions/33237074/request-over-wifi-on-android-m
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        ConnectivityManager connectivityManager = (ConnectivityManager)
                sm.mApplication.getSystemService(Context.CONNECTIVITY_SERVICE);
        for (Network net : connectivityManager.getAllNetworks()) {
            NetworkInfo netInfo = connectivityManager.getNetworkInfo(net);
            if (netInfo != null
                    && netInfo.getType() == ConnectivityManager.TYPE_WIFI
                    && netInfo.getExtraInfo() != null
                    && netInfo.getExtraInfo()
                    .equals(sm.mStateRegistry.wifiInfo.getSSID())) {
                connectivityManager.bindProcessToNetwork(net);
                break;
            }
        }
    }

    sm.mStateRegistry.apiAttempts = 1;
    sm.setCurrentState(State.CHECK_API);
}
 
Example 7
Source File: Network.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
public static boolean isCtwap(Context context)
{
    if (!"CN".equalsIgnoreCase(((TelephonyManager)context.getSystemService("phone")).getSimCountryIso()))
    {
        return false;
    }
    ConnectivityManager connectivitymanager = (ConnectivityManager)context.getSystemService("connectivity");
    if (connectivitymanager == null)
    {
        return false;
    }
    NetworkInfo networkinfo = connectivitymanager.getActiveNetworkInfo();
    if (networkinfo == null)
    {
        return false;
    }
    String s = networkinfo.getExtraInfo();
    if (TextUtils.isEmpty(s) || s.length() < 3)
    {
        return false;
    }
    return s.contains("ctwap");
}
 
Example 8
Source File: CommonUtils.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 获取当前网络类型
 *
 * @return 0:没有网络 1:WIFI网络 2:WAP网络 3:NET网络
 */
public static int getNetworkType(Context context) {
    int netType = 0;
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo == null) {
        return netType;
    }
    int nType = networkInfo.getType();
    if (nType == ConnectivityManager.TYPE_MOBILE) {
        String extraInfo = networkInfo.getExtraInfo();
        if (!TextUtils.isEmpty(extraInfo)) {
            if (extraInfo.toLowerCase(Locale.getDefault()).equals("cmnet")) {
                netType = NETTYPE_CMNET;
            } else {
                netType = NETTYPE_CMWAP;
            }
        }
    } else if (nType == ConnectivityManager.TYPE_WIFI) {
        netType = NETTYPE_WIFI;
    }
    return netType;
}
 
Example 9
Source File: DeviceUtils.java    From AudioManagerSDK with Apache License 2.0 6 votes vote down vote up
/**
 * 获取当前网络类型
 *
 * @return 0:没有网络 1:WIFI网络 2:WAP网络 3:NET网络
 */
public static int getNetworkType(Context context) {
    int netType = 0;
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo == null) {
        return netType;
    }
    int nType = networkInfo.getType();
    if (nType == ConnectivityManager.TYPE_MOBILE) {
        String extraInfo = networkInfo.getExtraInfo();
        if (extraInfo != null && !extraInfo.isEmpty()) {
            if (extraInfo.toLowerCase().equals("cmnet")) {
                netType = NETTYPE_CMNET;
            } else {
                netType = NETTYPE_CMWAP;
            }
        }
    } else if (nType == ConnectivityManager.TYPE_WIFI) {
        netType = NETTYPE_WIFI;
    }
    return netType;
}
 
Example 10
Source File: AndroidUtil.java    From NewsMe with Apache License 2.0 5 votes vote down vote up
/**
 * Cmwap网络是否已连接
 * 
 * @param context
 * @return
 */
public static boolean isNetworkConnectedByCmwap(Context context) {
	NetworkInfo networkInfo = ((ConnectivityManager) context
			.getSystemService(Context.CONNECTIVITY_SERVICE))
			.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
	return networkInfo != null && networkInfo.isConnected()
			&& networkInfo.getExtraInfo() != null
			&& networkInfo.getExtraInfo().toLowerCase().contains("cmwap");
}
 
Example 11
Source File: XLUtil.java    From MiniThunder with Apache License 2.0 5 votes vote down vote up
public static String getAPNName(Context context) {
    if (context == null) {
        return null;
    }
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
    if (connectivityManager == null) {
        return null;
    }
    NetworkInfo networkInfo = connectivityManager.getNetworkInfo(0);
    if (networkInfo == null) {
        return null;
    }
    return networkInfo.getExtraInfo();
}
 
Example 12
Source File: NetworkManager.java    From reader with MIT License 5 votes vote down vote up
/**
 * Get the latest network connection information
 *
 * @param info the current active network info
 * @return a JSONObject that represents the network info
 */
private JSONObject getConnectionInfo(NetworkInfo info) {
    String type = TYPE_NONE;
    String extraInfo = "";
    if (info != null) {
        // If we are not connected to any network set type to none
        if (!info.isConnected()) {
            type = TYPE_NONE;
        }
        else {
            type = getType(info);
        }
        extraInfo = info.getExtraInfo();
    }

    Log.d("CordovaNetworkManager", "Connection Type: " + type);
    Log.d("CordovaNetworkManager", "Connection Extra Info: " + extraInfo);

    JSONObject connectionInfo = new JSONObject();

    try {
        connectionInfo.put("type", type);
        connectionInfo.put("extraInfo", extraInfo);
    } catch (JSONException e) { }

    return connectionInfo;
}
 
Example 13
Source File: NetworkManager.java    From showCaseCordova with Apache License 2.0 5 votes vote down vote up
/**
 * Get the latest network connection information
 *
 * @param info the current active network info
 * @return a JSONObject that represents the network info
 */
private JSONObject getConnectionInfo(NetworkInfo info) {
    String type = TYPE_NONE;
    String extraInfo = "";
    if (info != null) {
        // If we are not connected to any network set type to none
        if (!info.isConnected()) {
            type = TYPE_NONE;
        }
        else {
            type = getType(info);
        }
        extraInfo = info.getExtraInfo();
    }

    Log.d("CordovaNetworkManager", "Connection Type: " + type);
    Log.d("CordovaNetworkManager", "Connection Extra Info: " + extraInfo);

    JSONObject connectionInfo = new JSONObject();

    try {
        connectionInfo.put("type", type);
        connectionInfo.put("extraInfo", extraInfo);
    } catch (JSONException e) { }

    return connectionInfo;
}
 
Example 14
Source File: NetworkManager.java    From ultimate-cordova-webview-app with MIT License 5 votes vote down vote up
/**
 * Get the latest network connection information
 *
 * @param info the current active network info
 * @return a JSONObject that represents the network info
 */
private JSONObject getConnectionInfo(NetworkInfo info) {
    String type = TYPE_NONE;
    String extraInfo = "";
    if (info != null) {
        // If we are not connected to any network set type to none
        if (!info.isConnected()) {
            type = TYPE_NONE;
        }
        else {
            type = getType(info);
        }
        extraInfo = info.getExtraInfo();
    }

    LOG.d(LOG_TAG, "Connection Type: " + type);
    LOG.d(LOG_TAG, "Connection Extra Info: " + extraInfo);

    JSONObject connectionInfo = new JSONObject();

    try {
        connectionInfo.put("type", type);
        connectionInfo.put("extraInfo", extraInfo);
    } catch (JSONException e) {
        LOG.d(LOG_TAG, e.getLocalizedMessage());
    }

    return connectionInfo;
}
 
Example 15
Source File: NetworkManager.java    From reader with MIT License 5 votes vote down vote up
/**
 * Get the latest network connection information
 *
 * @param info the current active network info
 * @return a JSONObject that represents the network info
 */
private JSONObject getConnectionInfo(NetworkInfo info) {
    String type = TYPE_NONE;
    String extraInfo = "";
    if (info != null) {
        // If we are not connected to any network set type to none
        if (!info.isConnected()) {
            type = TYPE_NONE;
        }
        else {
            type = getType(info);
        }
        extraInfo = info.getExtraInfo();
    }

    Log.d("CordovaNetworkManager", "Connection Type: " + type);
    Log.d("CordovaNetworkManager", "Connection Extra Info: " + extraInfo);

    JSONObject connectionInfo = new JSONObject();

    try {
        connectionInfo.put("type", type);
        connectionInfo.put("extraInfo", extraInfo);
    } catch (JSONException e) { }

    return connectionInfo;
}
 
Example 16
Source File: BaseApplication.java    From qingyang with Apache License 2.0 5 votes vote down vote up
/**
 * 获取当前网络类型
 * 
 * @return 0:没有网络 1:wifi网络 2:wap网络 3:net网络
 */
public int getNetworkType() {
	int netType = 0;

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

	NetworkInfo ni = cm.getActiveNetworkInfo();

	if (ni == null) {
		return netType;
	}

	int nType = ni.getType();

	if (nType == ConnectivityManager.TYPE_MOBILE) {
		String extraInfo = ni.getExtraInfo();
		if (!StringUtil.isEmpty(extraInfo)) {
			// 如果小写等于cmnet
			if (extraInfo.toLowerCase().equals("cmnet")) {
				netType = NETTYPE_CMNET;
			} else {
				netType = NETTYPE_CMWAP;
			}
		}
	} else if (nType == ConnectivityManager.TYPE_WIFI) {
		netType = NETTYPE_WIFI;
	}

	return netType;
}
 
Example 17
Source File: DeviceStateReceiver.java    From Cybernet-VPN with GNU General Public License v3.0 4 votes vote down vote up
public void networkStateChange(Context context) {
     NetworkInfo networkInfo = getCurrentNetworkInfo(context);
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
     boolean sendusr1 = prefs.getBoolean("netchangereconnect", true);
     String netstatestring;
     if (networkInfo == null) {
         netstatestring = "not connected";
     } else {
         String subtype = networkInfo.getSubtypeName();
         if (subtype == null) subtype = "";
         String extrainfo = networkInfo.getExtraInfo();
         if (extrainfo == null) extrainfo = "";
         /*
         if(networkInfo.getType()==android.net.ConnectivityManager.TYPE_WIFI) {
	WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
	WifiInfo wifiinfo = wifiMgr.getConnectionInfo();
	extrainfo+=wifiinfo.getBSSID();
	subtype += wifiinfo.getNetworkId();
}*/
         netstatestring = String.format("%2$s %4$s to %1$s %3$s", networkInfo.getTypeName(), networkInfo.getDetailedState(), extrainfo, subtype);
     }
     if (networkInfo != null && networkInfo.getState() == State.CONNECTED) {
         int newnet = networkInfo.getType();
         boolean pendingDisconnect = (network == connectState.PENDINGDISCONNECT);
         network = connectState.SHOULDBECONNECTED;
         boolean sameNetwork;
         if (lastConnectedNetwork == null || lastConnectedNetwork.getType() != networkInfo.getType() || !equalsObj(lastConnectedNetwork.getExtraInfo(), networkInfo.getExtraInfo()))
             sameNetwork = false;
         else sameNetwork = true;
         /* Same network, connection still 'established' */
         if (pendingDisconnect && sameNetwork) {
             mDisconnectHandler.removeCallbacks(mDelayDisconnectRunnable);
             // Reprotect the sockets just be sure
             mManagement.networkChange(true);
         } else {
             /* Different network or connection not established anymore */
             if (screen == connectState.PENDINGDISCONNECT) screen = connectState.DISCONNECTED;
             if (shouldBeConnected()) {
                 mDisconnectHandler.removeCallbacks(mDelayDisconnectRunnable);
                 if (pendingDisconnect || !sameNetwork) mManagement.networkChange(sameNetwork);
                 else mManagement.resume();
             }
             lastNetwork = newnet;
             lastConnectedNetwork = networkInfo;
         }
     } else if (networkInfo == null) {
         // Not connected, stop openvpn, set last connected network to no network
         lastNetwork = -1;
         if (sendusr1) {
             network = connectState.PENDINGDISCONNECT;
             mDisconnectHandler.postDelayed(mDelayDisconnectRunnable, DISCONNECT_WAIT * 1000);
         }
     }
     if (!netstatestring.equals(lastStateMsg)) VpnStatus.logInfo(R.string.netstatus, netstatestring);
     lastStateMsg = netstatestring;
 }
 
Example 18
Source File: NetworkConnectChangedReceiver.java    From AndroidDemo with MIT License 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {// 这个监听wifi的打开与关闭,与wifi的连接无关

    if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
        int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
        Log.e(TAG0, "WIFI状态:" + wifiState);
        switch (wifiState) {
            case WifiManager.WIFI_STATE_DISABLED:
                //APP.getInstance().setEnablaWifi(false);
                Log.e(TAG0,"WIFI_STATE_DISABLED" );
                break;
            case WifiManager.WIFI_STATE_DISABLING:
                Log.e(TAG0,"WIFI_STATE_DISABLING" );
                break;
            case WifiManager.WIFI_STATE_ENABLING:
                Log.e(TAG0,"WIFI_STATE_ENABLING" );
                break;
            case WifiManager.WIFI_STATE_ENABLED:
                //APP.getInstance().setEnablaWifi(true);
                Log.e(TAG0,"WIFI_STATE_ENABLED");
                break;
            case WifiManager.WIFI_STATE_UNKNOWN:
                Log.e(TAG0,"WIFI_STATE_UNKNOWN" );
                break;
            default:
                break;
        }
    }
    // 这个监听wifi的连接状态即是否连上了一个有效无线路由,当上边广播的状态是WifiManager
    // .WIFI_STATE_DISABLING,和WIFI_STATE_DISABLED的时候,根本不会接到这个广播。
    // 在上边广播接到广播是WifiManager.WIFI_STATE_ENABLED状态的同时也会接到这个广播,
    // 当然刚打开wifi肯定还没有连接到有效的无线
    if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
        Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if (null != parcelableExtra) {
            NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
            NetworkInfo.State state = networkInfo.getState();
            boolean isConnected = state == NetworkInfo.State.CONNECTED;// 当然,这边可以更精确的确定状态
            Log.e(TAG1, "WIFI连接状态:" + isConnected);
            if (isConnected) {
                //APP.getInstance().setWifi(true);
            } else {
               // APP.getInstance().setWifi(false);
            }
        }
    }
    // 这个监听网络连接的设置,包括wifi和移动数据的打开和关闭。.
    // 最好用的还是这个监听。wifi如果打开,关闭,以及连接上可用的连接都会接到监听。
    // 这个广播的最大弊端是比上边两个广播的反应要慢,如果只是要监听wifi,觉得还是用上边两个配合比较合适。
    if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        Log.i(TAG2, "CONNECTIVITY_ACTION");
        string = "\n您的手机当前网络状态是:\n\n";
        NetworkInfo activeNetwork = manager.getActiveNetworkInfo();
        if (activeNetwork != null) { // connected to the internet
            if (activeNetwork.isConnected()) {
                if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                    // connected to wifi
                    string += "当前WiFi连接可用 ";
                } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                    // connected to the mobile provider's data plan
                    string += "当前移动网络连接可用 ";
                }
            } else {
                string += "当前没有网络连接,请确保你已经打开网络 ";
            }

            string +=   "\n类型名:"        + activeNetwork.getTypeName()+
                        "\n子类型名:"       + activeNetwork.getSubtypeName()+
                        "\n状态:"           + activeNetwork.getState()+
                        "\n详细状态:"       + activeNetwork.getDetailedState().name()+
                        "\n额外状态:"       + activeNetwork.getExtraInfo()+
                        "\n类型:"           + activeNetwork.getType();
        } else {   // not connected to the internet
            string += "当前没有网络连接,请确保你已经打开网络 ";
        }
        Log.e("String:",string);
        brInteraction.setText(string);

    }

}
 
Example 19
Source File: NetBasicInfo.java    From AndroidHttpCapture with MIT License 4 votes vote down vote up
public String getApnInfo() {
    TelephonyManager tel = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
    String opCode = tel.getSimOperator();

    String operatorName;
    if (opCode.startsWith("46000") || opCode.startsWith("46002")) {
        operatorName = "中国移动";
    } else if (opCode.equals("46001")) {
        operatorName = "中国联通";
    } else if (opCode.equals("46003")) {
        operatorName = "中国电信";
    } else {
        operatorName = "未知";
    }

    ConnectivityManager mag = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mobInfo = mag.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    NetworkInfo wifiInfo = mag.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    //NetworkInfo mobInfo = mag.getActiveNetworkInfo();

    StringBuilder mOutputString = new StringBuilder();

    mOutputString.append("MNC Code Info:\n");
    mOutputString.append("IMSI=" + opCode + " <" + operatorName + ">\n");

    mOutputString.append("\nMobile Network Info:\n");

    //通过ExtraInfo 获取移动网络运营商信息
    if (mobInfo != null && mobInfo.getExtraInfo() != null) {
        mOutputString.append("运营商类型:");
        if (mobInfo.getExtraInfo().equals(APN_CMWAP) || mobInfo.getExtraInfo().equals(APN_CMNET)) {
            mOutputString.append("移动");
        } else if (mobInfo.getExtraInfo().equals(APN_UNIWAP) || mobInfo.getExtraInfo().equals(APN_UNINET)
                || mobInfo.getExtraInfo().equals(APN_UNI3gWAP) || mobInfo.getExtraInfo().equals(APN_UNI3gNET)) {
            mOutputString.append("联通");
        } else if (mobInfo.getExtraInfo().equals(APN_CTWAP) || mobInfo.getExtraInfo().equals(APN_CTNET)
                || mobInfo.getExtraInfo().equals(APN_CTLTE)) {
            mOutputString.append("电信");
        } else {
            mOutputString.append(operatorName);
        }

        if (mobInfo.getExtraInfo().contains("wap")) {
            mOutputString.append("--Wap");
        } else if (mobInfo.getExtraInfo().contains("net")) {
            mOutputString.append("--Net");
        } else {
            mOutputString.append("--Unkown");
        }


        mOutputString.append("\n网络类型:");

        int netType = getNetworkClass(mobInfo.getSubtype());
        switch (netType) {
            case NETWORK_CLASS_2_G:
                mOutputString.append("2G\n");
                break;
            case NETWORK_CLASS_3_G:
                mOutputString.append("3G\n");
                break;
            case NETWORK_CLASS_4_G:
                mOutputString.append("4G\n");
                break;
            default:
                mOutputString.append("未知\n");
                break;
        }
    }


    if (mobInfo != null) {
        mOutputString.append("ExtraInfo=" + mobInfo.getExtraInfo() + "\n");
        mOutputString.append("SubtypeName=" + mobInfo.getSubtypeName() + "  SubType = " + mobInfo.getSubtype() + "\n");
        mOutputString.append("TypeName=" + mobInfo.getTypeName() + "  Type = " + mobInfo.getType() + "\n");
    }
    mOutputString.append("\nWIFI Network Info:\n");
    mOutputString.append("ExtraInfo=" + wifiInfo.getExtraInfo() + "\n");
    mOutputString.append("SubtypeName=" + wifiInfo.getSubtypeName() + "  SubType = " + wifiInfo.getSubtype() + "\n");
    mOutputString.append("TypeName=" + wifiInfo.getTypeName() + "  Type = " + wifiInfo.getType() + "\n");

    mOutputString.append("\nIP Info:\n");
    mOutputString.append("IPv4 Address=" + GetIp(true) + "\n");
    mOutputString.append("IPv6 Address=" + GetIp(false) + "\n");
    mOutputString.append("DNS Address=" + getLocalDNS() + "\n");

    return mOutputString.toString();
}
 
Example 20
Source File: AppNetworkMgr.java    From AndroidWallet with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 获取当前手机连接的网络类型
 *
 * @param context 上下文
 * @return int 网络类型
 */
public static int getNetworkState(Context context) {
    //获取ConnectivityManager对象
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    //获得当前网络信息
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isAvailable()) {
        //获取网络类型
        int currentNetWork = networkInfo.getType();
        //手机网络类型
        if (currentNetWork == ConnectivityManager.TYPE_MOBILE) {
            if (networkInfo.getExtraInfo() != null) {
                if (networkInfo.getExtraInfo().equals("cmnet")) {
                    KLog.i("AppNetworkMgr", "当前网络为中国移动CMNET网络");
                    return TYPE_MOBILE_CMNET;
                }
                if (networkInfo.getExtraInfo().equals("cmwap")) {
                    KLog.i("AppNetworkMgr", "当前网络为中国移动CMWAP网络");
                    return TYPE_MOBILE_CMWAP;
                }
                if (networkInfo.getExtraInfo().equals("uniwap")) {
                    KLog.i("AppNetworkMgr", "当前网络为中国联通UNIWAP网络");
                    return TYPE_MOBILE_UNIWAP;
                }
                if (networkInfo.getExtraInfo().equals("3gwap")) {
                    KLog.i("AppNetworkMgr", "当前网络为中国联通3GWAP网络");
                    return TYPE_MOBILE_3GWAP;
                }
                if (networkInfo.getExtraInfo().equals("3gnet")) {
                    KLog.i("AppNetworkMgr", "当前网络为中国联通3GNET网络");
                    return TYPE_MOBLIE_3GNET;
                }
                if (networkInfo.getExtraInfo().equals("uninet")) {
                    KLog.i("AppNetworkMgr", "当前网络为中国联通UNINET网络");
                    return TYPE_MOBILE_UNINET;
                }
                if (networkInfo.getExtraInfo().equals("ctwap")) {
                    KLog.i("AppNetworkMgr", "当前网络为中国电信CTWAP网络");
                    return TYPE_MOBILE_UNINET;
                }
                if (networkInfo.getExtraInfo().equals("ctnet")) {
                    KLog.i("AppNetworkMgr", "当前网络为中国电信CTNET网络");
                    return TYPE_MOBILE_UNINET;
                }
            }
            //WIFI网络类型
        } else if (currentNetWork == ConnectivityManager.TYPE_WIFI) {
            KLog.i("AppNetworkMgr", "当前网络为WIFI网络");
            return TYPE_WIFI;
        }
    }
    KLog.i("AppNetworkMgr-->>NetworkUtils", "当前网络为不是我们考虑的网络");
    return TYPE_NO;
}