android 中怎么判斷網(wǎng)絡(luò)連接,很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
成都服務(wù)器托管,創(chuàng)新互聯(lián)提供包括服務(wù)器租用、成都西云數(shù)據(jù)中心、帶寬租用、云主機(jī)、機(jī)柜租用、主機(jī)租用托管、CDN網(wǎng)站加速、空間域名等業(yè)務(wù)的一體化完整服務(wù)。電話咨詢:13518219792
//判斷是否為wifi連接 public boolean isWifiConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mWiFiNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (mWiFiNetworkInfo != null) { return mWiFiNetworkInfo.isAvailable(); } } return false; } //判斷是否為網(wǎng)絡(luò)連接是否正常 public boolean isNetworkConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager .getActiveNetworkInfo(); if (mNetworkInfo != null) { return mNetworkInfo.isAvailable(); } } return false; } //判斷網(wǎng)絡(luò)連接類型 public static int getConnectedType(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager .getActiveNetworkInfo(); if (mNetworkInfo != null && mNetworkInfo.isAvailable()) { return mNetworkInfo.getType(); } } return -1; } //判斷手機(jī)sim卡類型 public static String get(Context context) { TelephonyManager telManager = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); String operator = telManager.getSimOperator(); if (operator != null) { if (operator.equals("46000") || operator.equals("46002")) { // 中國移動(dòng) return "移動(dòng)"; } else if (operator.equals("46001")) { return "聯(lián)通"; // 中國聯(lián)通 } else if (operator.equals("46003")) { return "電信"; // 中國電信 } } return ""; } //判斷手機(jī)是否能正常上網(wǎng) 進(jìn)行ping操作, 即使判斷手機(jī)連接了wifi了(熱點(diǎn)沒有上網(wǎng)),手機(jī)還是不能上網(wǎng) private static final boolean ping() { String result = null; try { String ip = "www.baidu.com";// 除非百度掛了,否則用這個(gè)應(yīng)該沒問題~ Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + ip);//ping3次 // 讀取ping的內(nèi)容,可不加。 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.i("TTT", "result content : " + stringBuffer.toString()); // PING的狀態(tài) int status = p.waitFor(); if (status == 0) { result = "successful~"; return true; } else { result = "failed~ cannot reach the IP address"; } } catch (IOException e) { result = "failed~ IOException"; } catch (InterruptedException e) { result = "failed~ InterruptedException"; } finally { Log.i("TTT", "result = " + result); } return false; }
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。