您好,登錄后才能下訂單哦!
public class NetUtil { public static final int NETWORN_NONE = 0;//無網絡 public static final int NETWORN_WIFI = 1;//wife public static final int NETWORN_MOBILE = 2;//手機 /** * 獲取當前網絡的狀態 * @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; } /** * 判斷是否有網絡 * @param context * @return */ public static boolean isNetConnected(Context context) { boolean isNetConnected; // 獲得網絡連接服務 ConnectivityManager connManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = connManager.getActiveNetworkInfo(); if (info != null && info.isAvailable()) { // String name = info.getTypeName(); // L.i("當前網絡名稱:" + name); isNetConnected = true; } else { L.i("沒有可用網絡"); isNetConnected = false; } return isNetConnected; } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。