用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

Android工具类-关于网络、状态的工具类

2017-03-02 作者: 云代码会员举报

[android]代码库

    public class NetworkUtils { 
       
        /**
         * 网络是否可用
         
         * @param activity
         * @return
         */ 
        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
        
       
       
        /**
         * Gps是否打开
         
         * @param context
         * @return
         */ 
        public static boolean isGpsEnabled(Context context) { 
            LocationManager locationManager = ((LocationManager) context 
                    .getSystemService(Context.LOCATION_SERVICE)); 
            List<String> accessibleProviders = locationManager.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); 
        
       
        /**
         * 判断当前网络是否是wifi网络
         * if(activeNetInfo.getType()==ConnectivityManager.TYPE_MOBILE) { 
         
         * @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
        
    
 
以上方法均通过测试,tv_result为自设定的TextView。
 
  
 
        tv_result.append("网络是否可用:"+NetworkUtils.isNetworkAvailable(MainActivity.this)+"\n"); 
        tv_result.append("GPS开关是否打开:"+NetworkUtils.isGpsEnabled(MainActivity.this)+"\n"); 
        tv_result.append("是否为3G网络:"+NetworkUtils.is3G(MainActivity.this)+"\n"); 
        tv_result.append("WIFI是否打开:"+NetworkUtils.isWifiEnabled(MainActivity.this)+"\n"); 
        tv_result.append("是否为WIFI网络:"+NetworkUtils.isWifi(MainActivity.this)+"\n");


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...