4GQoS测速应用总结(一)

此文是为了记录一下在开发4GQoS测速应用中用到的一些方法。

1、将背景图和状态栏融合到一起:

 if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
2、获取本机Ip地址:

public String getLocalIpAddress() {
        //Enumeration是一个接口,NetworkInterface.getNetworkInterfaces() 返回的是一个泛型的枚举器,元素类型是NetworkInterface
        //NetworkInterface表示物理硬件和虚拟地址
        try {
            Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
            while (en.hasMoreElements()) {
                NetworkInterface ni = en.nextElement();
                Enumeration<InetAddress> enIp = ni.getInetAddresses();
                while (enIp.hasMoreElements()) {
                    //InetAddress是ip地址的高级表示
                    InetAddress inet = enIp.nextElement();
                    //isLoopbackAddress():指示指定的ip地址是否为回环地址
                    if (!inet.isLoopbackAddress() && inet instanceof Inet4Address) {
                        return inet.getHostAddress().toString();
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return "0.0.0.0";
    }
3、获取手机唯一标识:

public String getLocalDeviceId(Context context){
        TelephonyManager tm = (TelephonyManager)context.getSystemService(context.TELEPHONY_SERVICE);
        String number = tm.getDeviceId();
        return number;
    }
注:要获取本机号码,可将getDeviceId()改为getLine1Number(),但是此方法有时不一定能用,因为运营商为了保护客户隐私,可能不会提供手机号码。

4、Ping命令:

/**
     * ping ip, 然后获得时延,抖动,丢包率值
     *
     * @param ipAddress
     * @param coutCmd
     * @return ArrayList<String>
     */
    private ArrayList<String> pingIpAddress(String ipAddress, String coutCmd) {
        ArrayList<String> array = new ArrayList<>();
        final String ping = "ping -c " + coutCmd + " " + ipAddress;
        InputStream in = null;
        BufferedReader reader = null;
        try {
            process = Runtime.getRuntime().exec(ping);
            in = process.getInputStream();
            pingStatus = process.waitFor();
            reader = new BufferedReader(new InputStreamReader(in));
            String lineStr;
            if (pingStatus == 0) {
                //ping成功
                while ((lineStr = reader.readLine()) != null) {
                    Log.i(TAG, "pingIpAddress: " + lineStr);
                    if (lineStr.contains("packet loss")) {
                        int i = lineStr.indexOf("received");
                        int j = lineStr.indexOf("%");
                        lost = lineStr.substring(i + 10, j + 1);
                        array.add(lost);
                    }
                    if (lineStr.contains("avg")) {
                        int i = lineStr.indexOf("/", 20);
                        int j = lineStr.indexOf(".", i);
                        delay = lineStr.substring(i + 1, j);
                        array.add(delay);
                    }
                    if (lineStr.contains("mdev")) {
                        mdev = lineStr.substring(lineStr.lastIndexOf("/") + 1, lineStr.indexOf("ms"));
                        mdev = mdev.substring(0, mdev.indexOf(".") + 2);
                        array.add(mdev);
                    }
                }
                return array;
            } else {
                //ping失败
                while ((lineStr = reader.readLine()) != null) {
                    Log.i(TAG, "pingIpAddress: " + lineStr);
                    if (lineStr.contains("packet loss")) {
                        int i = lineStr.indexOf("received");
                        int j = lineStr.indexOf("%");
                        lost = lineStr.substring(i + 10, j + 1);
                        array.add(lost);
                    }
                }
                array.add("∞");
                array.add("∞");
                return array;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (process != null) {
                process.destroy();
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值