android中判斷網絡連接是否可用


一、判斷網絡連接是否可用

 

public static boolean isNetworkAvailable(Context context) {   
        ConnectivityManager cm = (ConnectivityManager) context   
                .getSystemService(Context.CONNECTIVITY_SERVICE);   
        if (cm == null) {   
        } else {
       //如果僅僅是用來判斷網絡連接
       //則可以使用 cm.getActiveNetworkInfo().isAvailable(); NetworkInfo[] info
= cm.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i++) { if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } } } return false; }

  二、判斷GPS是否打開

  public static boolean isGpsEnabled(Context context) {   
        LocationManager lm = ((LocationManager) context   
                .getSystemService(Context.LOCATION_SERVICE));   
        List<String> accessibleProviders = lm.getProviders(true);   
        return accessibleProviders != null && accessibleProviders.size() > 0;   
    } 

三、判斷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);   
    } 

四、判斷是否是3G網絡

 

    public static boolean is3rd(Context context) {   
        ConnectivityManager cm = (ConnectivityManager) context   
                .getSystemService(Context.CONNECTIVITY_SERVICE);   
        NetworkInfo networkINfo = cm.getActiveNetworkInfo();   
        if (networkINfo != null   
                && networkINfo.getType() == ConnectivityManager.TYPE_MOBILE) {   
            return true;   
        }   
        return false;   
    }  

五、判斷是wifi還是3g網絡,用戶的體現性在這里了,wifi就可以建議下載或者在線播放。

  public static boolean isWifi(Context context) {   
            ConnectivityManager cm = (ConnectivityManager) context   
                    .getSystemService(Context.CONNECTIVITY_SERVICE);   
            NetworkInfo networkINfo = cm.getActiveNetworkInfo();   
            if (networkINfo != null   
                    && networkINfo.getType() == ConnectivityManager.TYPE_WIFI) {   
                return true;   
            }   
            return false;   
        }

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM