public class NetUtil {
public static final int NETWORN_NONE = 0;//無網(wǎng)絡
public static final int NETWORN_WIFI = 1;//wife
public static final int NETWORN_MOBILE = 2;//手機
/**
* 獲取當前網(wǎng)絡的狀態(tài)
* @param context
* @return
*/
public static int getNetworkState(Context context) {
ConnectivityManager connManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
// Wifi
State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.getState();
if (state == State.CONNECTED || state == State.CONNECTING) {
return NETWORN_WIFI;
}
// 3G
state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.getState();
if (state == State.CONNECTED || state == State.CONNECTING) {
return NETWORN_MOBILE;
}
return NETWORN_NONE;
}
/**
* 判斷是否有網(wǎng)絡
* @param context
* @return
*/
public static boolean isNetConnected(Context context) {
boolean isNetConnected;
// 獲得網(wǎng)絡連接服務
ConnectivityManager connManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connManager.getActiveNetworkInfo();
if (info != null && info.isAvailable()) {
// String name = info.getTypeName();
// L.i("當前網(wǎng)絡名稱:" + name);
isNetConnected = true;
} else {
L.i("沒有可用網(wǎng)絡");
isNetConnected = false;
}
return isNetConnected;
}
}
當前題目:Android學習--判斷當前設備的網(wǎng)絡狀態(tài)
網(wǎng)址分享:
http://weahome.cn/article/gdjpoe.html