How to know device is connected to Wifi or 3G, programatically

*爱你&永不变心* 提交于 2019-12-18 03:43:37

问题


How i can know device is connected to Wifi or 3G, programmatically

Thanks


回答1:


you can use WifiManager class as mentioned here

Edit: by calling getConnectionInfo() function of WifiManager class you will get WifiInfo object

WifiInfo has function getBSSID() which gives you connected AP's name

if its null that means it is not connected to any AP via Wifi ( Wifi is not enabled )

btw while looking for more info, i found this which should answer all your questions about mobile connectivity and wifi connectivity




回答2:


here is my working sample:

public boolean isNetworkTypeMobile() {
    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    return (cm!=null && cm.getActiveNetworkInfo()!=null && isNetworkTypeMobile(cm.getActiveNetworkInfo().getType()));
}

public static boolean isNetworkTypeMobile(int networkType) {
    switch (networkType) {
        case ConnectivityManager.TYPE_MOBILE: //0
        case ConnectivityManager.TYPE_MOBILE_MMS: //2
        case ConnectivityManager.TYPE_MOBILE_SUPL: //3
        case ConnectivityManager.TYPE_MOBILE_DUN: //4
        case ConnectivityManager.TYPE_MOBILE_HIPRI: //5
        case 10:
        case 11:
        case 12:
        case 14:
            return true;
        default:
            return false;
    }
}


来源:https://stackoverflow.com/questions/3461227/how-to-know-device-is-connected-to-wifi-or-3g-programatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!