安卓 判断某个应用是否有联网权限

 原理类似 电脑ping命令

    public static boolean isNetworkOnline() {

        Runtime runtime = Runtime.getRuntime();
        Process ipProcess = null;
        try {
            ipProcess = runtime.exec("ping -c 5 -w 4 223.5.5.5");
            InputStream input = ipProcess.getInputStream();

            BufferedReader in = new BufferedReader(new InputStreamReader(input));
            StringBuffer stringBuffer = new StringBuffer();
            String content = "";
            while ((content = in.readLine()) != null) {
                stringBuffer.append(content);
            }

            int exitValue = ipProcess.waitFor();
            if (exitValue == 0) {
                //WiFi连接,网络正常
                return true;
            } else {
                if (stringBuffer.indexOf("100% packet loss") != -1) {
                    XLog.showArgsInfo("网络丢包严重,判断为网络未连接");
                    return false;
                } else {
                    XLog.showArgsInfo("网络未丢包,判断为网络连接");
                    return true;
                }
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (ipProcess != null) {
                ipProcess.destroy();
            }
            runtime.gc();
        }
        return false;
    }

https://blog.csdn.net/yyanjun/article/details/81197991

参考

 public class Ping extends AsyncTask<String, Boolean, Boolean> {
        @Override
        protected Boolean doInBackground(String... strings) {
            try {
                Process p = Runtime.getRuntime().exec("ping -c 1 -w 3 " + strings[0]);
                InputStream input = p.getInputStream();
                InputStreamReader isr = new InputStreamReader(input);
                BufferedReader br = new BufferedReader(isr);
                String line;
                StringBuilder builder = new StringBuilder();
                while ((line = br.readLine()) != null) {
                    builder.append(line);
                }
                br.close();
                isr.close();
                input.close();
                br.close();
                Logs.i("返回的数据:"+builder+"  对比:"+builder.toString().contains("ttl"));
                /*如果Ip地址Ping成功后数据里面会有ttl这个数据,所以对比这个字符串即可*/
                return builder.toString().contains("ttl");
            } catch (MalformedURLException e) {
                Logs.e(e.toString());
                return false;
            } catch (IOException e) {
                Logs.e(e.toString());
                return false;
            }
        }
 
 
        @Override
        protected void onPostExecute(Boolean isPing) {
            Logs.v("Ping的结果:" + isPing);
            if(isPing){
                /*设备Ip地址ping成功后向后台发送数据*/
                if (!TextUtils.isEmpty(cllx)) {
                    if (INSPECT_START.equals(cllx)) {
                        //查验开始
                        inspectStart();
                    } else if (INSPECT_END.equals(cllx)) {
                        //查验结束
                        inspectEnd();
                    }
                }
//                DialogNoticeUtil.show(RequestAction.this,"执法记录仪连接成功");
            }else {
                DialogNoticeUtil.show(RequestAction.this,"执法记录仪连接失败");
            }
        }
}

————————————————
版权声明:本文为CSDN博主「ChampionDragon」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xxdw1992/article/details/103582908

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值