Android 网络工具类

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
public class NetworkCheck {
    /**  
     * 网络是否可用  
     */
    public static boolean isNetworkAvailable(Context context) {   
        ConnectivityManager connectivity = (ConnectivityManager) context   
                    .getSystemService(Context.CONNECTIVITY_SERVICE);   
        if (connectivity == null) {   
        } else {   
            NetworkInfo[] info = connectivity.getAllNetworkInfo();   
            if (info != null) {   
                for (int i = 0; i < info.length; i++) {   
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {   
                        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);
    }

     /**  
     * 判断当前网络是否是wifi网络  
     * if(activeNetInfo.getType()==ConnectivityManager.TYPE_MOBILE) { //判断3G网  
     *   
     * @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;   
    }   

    /**  
     * 判断当前网络是否是3G网络  
     *   
     * @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;   
    }   

}

标签: 网络

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:Android实现烟花效果

下一篇:Java实现简单的截图工具