加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

android 网络判断工具类(APN+WIFI)

发布时间:2020-12-15 00:23:10 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 public class NetWorkHelper { private static String LOG_TAG = "NetWorkHelper"; public static Uri uri = Uri.parse("content://telephony/carrier

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

    public class NetWorkHelper {  
      
        private static String LOG_TAG = "NetWorkHelper";  
      
        public static Uri uri = Uri.parse("content://telephony/carriers");  
      
        /** 
         * 判断是否有网络连接 
         */  
        public static boolean isNetworkAvailable(Context context) {  
            ConnectivityManager connectivity = (ConnectivityManager) context  
                    .getSystemService(Context.CONNECTIVITY_SERVICE);  
      
            if (connectivity == null) {  
                Log.w(LOG_TAG,"couldn't get connectivity manager");  
            } else {  
                NetworkInfo[] info = connectivity.getAllNetworkInfo();  
                if (info != null) {  
                    for (int i = 0; i < info.length; i++) {  
                        if (info[i].isAvailable()) {  
                            Log.d(LOG_TAG,"network is available");  
                            return true;  
                        }  
                    }  
                }  
            }  
            Log.d(LOG_TAG,"network is not available");  
            return false;  
        }  
          
        public static boolean checkNetState(Context context){  
            boolean netstate = false;  
            ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);  
            if(connectivity != null)  
            {  
                NetworkInfo[] info = connectivity.getAllNetworkInfo();  
                if (info != null) {  
                    for (int i = 0; i < info.length; i++)  
                    {  
                        if (info[i].getState() == NetworkInfo.State.CONNECTED)   
                        {  
                            netstate = true;  
                            break;  
                        }  
                    }  
                }  
            }  
            return netstate;  
        }  
      
        /** 
         * 判断网络是否为漫游 
         */  
        public static boolean isNetworkRoaming(Context context) {  
            ConnectivityManager connectivity = (ConnectivityManager) context  
                    .getSystemService(Context.CONNECTIVITY_SERVICE);  
            if (connectivity == null) {  
                Log.w(LOG_TAG,"couldn't get connectivity manager");  
            } else {  
                NetworkInfo info = connectivity.getActiveNetworkInfo();  
                if (info != null  
                        && info.getType() == ConnectivityManager.TYPE_MOBILE) {  
                    TelephonyManager tm = (TelephonyManager) context  
                            .getSystemService(Context.TELEPHONY_SERVICE);  
                    if (tm != null && tm.isNetworkRoaming()) {  
                        Log.d(LOG_TAG,"network is roaming");  
                        return true;  
                    } else {  
                        Log.d(LOG_TAG,"network is not roaming");  
                    }  
                } else {  
                    Log.d(LOG_TAG,"not using mobile network");  
                }  
            }  
            return false;  
        }  
      
        /** 
         * 判断MOBILE网络是否可用 
         *  
         * @param context 
         * @return 
         * @throws Exception 
         */  
        public static boolean isMobileDataEnable(Context context) throws Exception {  
            ConnectivityManager connectivityManager = (ConnectivityManager) context  
                    .getSystemService(Context.CONNECTIVITY_SERVICE);  
            boolean isMobileDataEnable = false;  
      
            isMobileDataEnable = connectivityManager.getNetworkInfo(  
                    ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();  
      
            return isMobileDataEnable;  
        }  
      
          
        /** 
         * 判断wifi 是否可用 
         * @param context 
         * @return 
         * @throws Exception 
         */  
        public static boolean isWifiDataEnable(Context context) throws Exception {  
            ConnectivityManager connectivityManager = (ConnectivityManager) context  
                    .getSystemService(Context.CONNECTIVITY_SERVICE);  
            boolean isWifiDataEnable = false;  
            isWifiDataEnable = connectivityManager.getNetworkInfo(  
                    ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();  
            return isWifiDataEnable;  
        }  
      
        /** 
         * 设置Mobile网络开关 
         *  
         * @param context 
         * @param enabled 
         * @throws Exception 
         */  
        public static void setMobileDataEnabled(Context context,boolean enabled)  
                throws Exception {  
            APNManager apnManager=APNManager.getInstance(context);  
            List<APN> list = apnManager.getAPNList();  
            if (enabled) {  
                for (APN apn : list) {  
                    ContentValues cv = new ContentValues();  
                    cv.put("apn",apnManager.matchAPN(apn.apn));  
                    cv.put("type",apnManager.matchAPN(apn.type));  
                    context.getContentResolver().update(uri,cv,"_id=?",new String[] { apn.apnId });  
                }  
            } else {  
                for (APN apn : list) {  
                    ContentValues cv = new ContentValues();  
                    cv.put("apn",apnManager.matchAPN(apn.apn) + "mdev");  
                    cv.put("type",apnManager.matchAPN(apn.type) + "mdev");  
                    context.getContentResolver().update(uri,new String[] { apn.apnId });  
                }  
            }  
        }  
      
    }  


以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读