android 中怎么判斷網(wǎng)絡(luò)連接,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(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")) {
// 中國移動
return "移動";
} 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";// 除非百度掛了,否則用這個應(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)的支持。
當(dāng)前題目:android中怎么判斷網(wǎng)絡(luò)連接
當(dāng)前網(wǎng)址:http://www.chinadenli.net/article36/gegdpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、全網(wǎng)營銷推廣、面包屑導(dǎo)航、做網(wǎng)站、ChatGPT、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)