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; } }
网站名称:Android学习--判断当前设备的网络状态
网页地址:https://www.cdcxhl.com/article4/gdjpoe.html
成都网站建设公司_创新互联,为您提供网站建设、品牌网站制作、面包屑导航、网站制作、做网站、电子商务
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联