zoukankan      html  css  js  c++  java
  • 安卓学习-其他-网络状态工具

    package org.excsoft.g;
    
    import java.io.IOException;
    import java.net.HttpURLConnection;
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.URL;
    import java.util.Enumeration;
    
    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.telephony.TelephonyManager;
    
    public class NetStatus {
    
        public static int NET_CNNT_BAIDU_OK = 1; // 正常访问因特网状态
        public static int NET_CNNT_BAIDU_TIMEOUT = 2; // 无法访问因特网状态
        public static int NET_NOT_PREPARE = 3; // 网络未准备好
        public static int NET_ERROR = 4;
        private static int TIMEOUT = 3000;
    
        /**
         * 返回当前网络状态
         * 
         * @param context
         * @return
         */
        public static int getNetState(Context context,String url) {
            try {
                ConnectivityManager connectivity = (ConnectivityManager) context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
                if (connectivity != null) {
                    NetworkInfo networkinfo = connectivity.getActiveNetworkInfo();
                    if (networkinfo != null) {
                        if (networkinfo.isAvailable() && networkinfo.isConnected()) {
                            if (!connectionNetwork(url))
                                return NET_CNNT_BAIDU_TIMEOUT;
                            else
                                return NET_CNNT_BAIDU_OK;
                        } else {
                            return NET_NOT_PREPARE;
                        }
                    }
                }
            } catch (Exception e) {
            }
            return NET_ERROR;
        }
        
        public static int getNetState1(Context context) {
            try {
                ConnectivityManager connectivity = (ConnectivityManager) context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
                if (connectivity != null) {
                    NetworkInfo networkinfo = connectivity.getActiveNetworkInfo();
                    if (networkinfo != null) {
                        if (networkinfo.isAvailable() && networkinfo.isConnected()) {
                            return NET_CNNT_BAIDU_OK;
                        } else {
                            return NET_NOT_PREPARE;
                        }
                    }
                }
            } catch (Exception e) {
            }
            return NET_ERROR;
        }
    
        /**
         * 拼百度地址
         * 
         * @return
         */
        static private boolean connectionNetwork(String url) {
            boolean result = false;
            HttpURLConnection httpUrl = null;
            try {
                httpUrl = (HttpURLConnection) new URL(url)
                        .openConnection();
                httpUrl.setConnectTimeout(TIMEOUT);
                httpUrl.connect();
                result = true;
            } catch (IOException e) {
            } finally {
                if (null != httpUrl) {
                    httpUrl.disconnect();
                }
                httpUrl = null;
            }
            return result;
        }
    
        /**
         * 判断当前网络是否是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;
        }
    
        /**
         * 判断当前网络是否是wifi网络
         * 
         * @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;
        }
    
        /**
         * 判断当前网络是否是2G网络
         * 
         * @param context
         * @return boolean
         */
        public static boolean is2G(Context context) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
            if (activeNetInfo != null
                    && (activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE
                            || activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS ||
    
                    activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_CDMA)) {
                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);
        }
    
        /**
         * 获得本机ip地址
         * 
         * @return
         */
        public static String GetHostIp() {
            try {
                for (Enumeration<NetworkInterface> en = NetworkInterface
                        .getNetworkInterfaces(); en.hasMoreElements();) {
                    NetworkInterface intf = en.nextElement();
                    for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr
                            .hasMoreElements();) {
                        InetAddress inetAddress = ipAddr.nextElement();
                        if (!inetAddress.isLoopbackAddress()) {
                            return inetAddress.getHostAddress();
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * 获取本机串号imei
         * 
         * @param context
         * @return
         */
        public static String getIMEI(Context context) {
            TelephonyManager telephonyManager = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            return telephonyManager.getDeviceId();
        }
    
    }
  • 相关阅读:
    页面跳转时,统计数据丢失问题探讨
    模拟form提交数据
    mysql学习问题解决
    20200824_小程序自动化测试与失败用例截图
    20200819_App自动化测试框架实战-这两天的很重要
    20200817_多点触摸操作和Hybrid应用自动化
    20200814_App元素定位&滑动操作
    20200812_adb常用命令&第一个App自动化测试脚本
    20200810_TestNG单元测试框架&App自动化环境部署
    20200807_Maven项目构建&TestNG单元测试框架
  • 原文地址:https://www.cnblogs.com/weijj/p/3978488.html
Copyright © 2011-2022 走看看