判断是否连接网络以及是否是局域网

    xiaoxiao2026-10-05  5

    判断是否能上外网 有时候我们连接上一个没有外网连接的WiFi或者需要输入账号和密码才能链接外网的网络,就会出现虽然网络可用,但是外网却不可以访问。针对这种情况,一般的解决办法就是ping一个外网,如果能ping通就说明可以真正上网,代码如下* @author sichard * @category 判断是否有外网连接(普通方法不能判断外网的网络是否连接,比如连接上局域网) * @return */ public static final boolean ping() { String result = null; try { String ip = "www.baidu.com";// ping 的地址,可以换成任何一种可靠的外网 Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + ip);// ping网址3次 // 读取ping的内容,可以不加 InputStream input = p.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(input)); StringBuffer stringBuffer = new StringBuffer(); String content = ""; while ((content = in.readLine()) != null) { stringBuffer.append(content); } Log.d("------ping-----", "result content : " + stringBuffer.toString()); // ping的状态 int status = p.waitFor(); if (status == 0) { result = "success"; return true; } else { result = "failed"; } } catch (IOException e) { result = "IOException"; } catch (InterruptedException e) { result = "InterruptedException"; } finally { Log.d("----result---", "result = " + result); } return false; 判断能否联网以及判断联网的类型           public static String GetNetworkType(){ String strNetworkType = ""; NetworkInfo networkInfo = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE).getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { strNetworkType = "WIFI"; } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) { String _strSubTypeName = networkInfo.getSubtypeName(); Log.e("cocos2d-x", "Network getSubtypeName : " + _strSubTypeName); // TD-SCDMA networkType is 17 int networkType = networkInfo.getSubtype(); 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: //api<8 : replace by 11 strNetworkType = "2G"; break; 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: //api<9 : replace by 14 case TelephonyManager.NETWORK_TYPE_EHRPD: //api<11 : replace by 12 case TelephonyManager.NETWORK_TYPE_HSPAP: //api<13 : replace by 15 strNetworkType = "3G"; break; case TelephonyManager.NETWORK_TYPE_LTE: //api<11 : replace by 13 strNetworkType = "4G"; break; default: // http://baike.baidu.com/item/TD-SCDMA 中国移动 联通 电信 三种3G制式 if (_strSubTypeName.equalsIgnoreCase("TD-SCDMA") || _strSubTypeName.equalsIgnoreCase("WCDMA") || _strSubTypeName.equalsIgnoreCase("CDMA2000")) { strNetworkType = "3G"; } else { strNetworkType = _strSubTypeName; } break; } Log.e("cocos2d-x", "Network getSubtype : " + Integer.valueOf(networkType).toString()); } } Log.e("cocos2d-x", "Network Type : " + strNetworkType); return strNetworkType;}
    转载请注明原文地址: https://ju.6miu.com/read-1312641.html
    最新回复(0)