Android 判斷網(wǎng)絡(luò)狀態(tài)實(shí)例詳解
Android 判斷網(wǎng)絡(luò)狀態(tài)實(shí)例詳解
實(shí)例代碼
package com.example.android;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.util.Enumeration;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
public class NetStatus {
public static int NET_CNNT_BAIDU_OK = 1; // 正常訪問因特網(wǎng)狀態(tài)
public static int NET_CNNT_BAIDU_TIMEOUT = 2; // 無法訪問因特網(wǎng)狀態(tài)
public static int NET_NOT_PREPARE = 3; // 網(wǎng)絡(luò)未準(zhǔn)備好
public static int NET_ERROR = 4;
private static int TIMEOUT = 3000;
/**
* 返回當(dāng)前網(wǎng)絡(luò)狀態(tài)
*
* @param context
* @return
*/
public static int getNetState(Context context) {
try {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo networkinfo = connectivity.getActiveNetworkInfo();
if (networkinfo != null) {
if (networkinfo.isAvailable() && networkinfo.isConnected()) {
if (!connectionNetwork())
return NET_CNNT_BAIDU_TIMEOUT;
else
return NET_CNNT_BAIDU_OK;
} else {
return NET_NOT_PREPARE;
}
}
}
} catch (Exception e) {
}
return NET_ERROR;
}
/**
* 拼百度地址
*
* @return
*/
private static boolean connectionNetwork() {
boolean result = false;
HttpURLConnection httpUrl = null;
try {
httpUrl = (HttpURLConnection) new URL("http://www.baidu.com").openConnection();
httpUrl.setConnectTimeout(TIMEOUT);
httpUrl.connect();
result = true;
} catch (IOException e) {
} finally {
if (null != httpUrl) {
httpUrl.disconnect();
}
httpUrl = null;
}
return result;
}
/**
* 判斷當(dāng)前網(wǎng)絡(luò)是否是3G網(wǎng)絡(luò)
*
* @param context
* @return boolean
*/
public static boolean is3G(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
return true;
}
return false;
}
/**
* 判斷當(dāng)前網(wǎng)絡(luò)是否是wifi網(wǎng)絡(luò)
*
* @param context
* @return boolean
*/
public static boolean isWifi(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
}
return false;
}
/**
* 判斷當(dāng)前網(wǎng)絡(luò)是否是2G網(wǎng)絡(luò)
*
* @param context
* @return boolean
*/
public static boolean is2G(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null && (activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE
|| activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS
|| activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_CDMA)) {
return true;
}
return false;
}
/**
* wifi是否打開
*/
public static boolean isWifiEnabled(Context context) {
ConnectivityManager mgrConn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager mgrTel = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return ((mgrConn.getActiveNetworkInfo() != null && mgrConn.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED)
|| mgrTel.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
}
/**
* 獲得本機(jī)ip地址
*
* @return
*/
public static String GetHostIp() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr.hasMoreElements();) {
InetAddress inetAddress = ipAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {
} catch (Exception e) {
}
return null;
}
/**
* 獲取本機(jī)串號imei
*
* @param context
* @return
*/
public static String getIMEI(Context context) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return telephonyManager.getDeviceId();
}
}
添加權(quán)限:訪問網(wǎng)絡(luò)權(quán)限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.INTERNET"/>
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Android仿QQ微信實(shí)時(shí)監(jiān)測網(wǎng)絡(luò)狀態(tài)
- Android BroadcastReceiver實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)實(shí)時(shí)監(jiān)聽
- Android檢查網(wǎng)絡(luò)狀態(tài)工具類詳解
- android 監(jiān)聽網(wǎng)絡(luò)狀態(tài)的變化及實(shí)戰(zhàn)的示例代碼
- Android判斷網(wǎng)絡(luò)狀態(tài)的代碼
- Android 判斷網(wǎng)絡(luò)狀態(tài)及開啟網(wǎng)路
- Android 監(jiān)聽網(wǎng)絡(luò)狀態(tài)方法詳解
- Android 廣播監(jiān)聽網(wǎng)絡(luò)狀態(tài)詳解及實(shí)例代碼
- Android中利用NetworkInfo判斷網(wǎng)絡(luò)狀態(tài)時(shí)出現(xiàn)空指針(NullPointerException)問題的解決方法
- Android使用觀察者模式Observer實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)監(jiān)聽
相關(guān)文章
Android中ListView下拉刷新的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了Android中ListView下拉刷新的實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2016-03-03
Android中實(shí)現(xiàn)下載和解壓zip文件功能代碼分享
這篇文章主要介紹了Android中實(shí)現(xiàn)下載和解壓zip文件功能代碼分享,本文直接給出了實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-03-03
Android藍(lán)牙庫FastBle的基礎(chǔ)入門使用
這篇文章主要給大家介紹了關(guān)于Android藍(lán)牙庫FastBle的基礎(chǔ)入門使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Android中Retrofit 2.0直接使用JSON進(jìn)行數(shù)據(jù)交互
本篇文章主要介紹了Android中Retrofit 2.0直接使用JSON進(jìn)行數(shù)據(jù)交互,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
Android Handler實(shí)現(xiàn)閃屏頁倒計(jì)時(shí)代碼
這篇文章主要介紹了Android Handler實(shí)現(xiàn)閃屏頁倒計(jì)時(shí)代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Libgdx解決部分Android機(jī)型鎖屏崩潰的方法
今天小編就為大家分享一篇關(guān)于Libgdx解決部分Android機(jī)型鎖屏崩潰的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
解決Android 沉浸式狀態(tài)欄和華為虛擬按鍵沖突問題
對于現(xiàn)在的 App 來說,布局頁面基本都會用到沉浸式狀態(tài)欄,單純的沉浸式狀態(tài)欄很容易解決,但是在華為手機(jī)上存在一個(gè)底部虛擬按鍵的問題,會導(dǎo)致頁面底部和頂部出現(xiàn)很大的問題,下面通過本文給大家分享Android 沉浸式狀態(tài)欄和華為虛擬按鍵沖突問題,一起看看吧2017-07-07

