【问题标题】:Detect if vpn service is on or not problem检测vpn服务是否开启
【发布时间】:2019-08-16 23:35:00
【问题描述】:

我在使用某些设备时遇到问题,例如某些 android box 或华为手机 如果用户使用数据包监控应用程序,我需要保护我的应用程序我所做的是如果用户在他们的设备上激活了 vpn,则不请求 jsonparse 但有些设备即使 vpn 不活跃 jsonparse 也不会运行。

这是我的代码

public static boolean isVpnConnectionActive() {
        List<String> networks = new ArrayList<>();

        try {
            for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
                if (networkInterface.isUp()) {
                    networks.add(networkInterface.getName());
                }if (networkInterface.getName().contains("tun0")||  networkInterface.getName().contains("ppp0")) {
                    System.exit(1);
                }

            }
        } catch (Exception ignored) {

        }
        return networks.contains("tun0");


    }

我在启动和创建时请求 jsonParse 以避免用户在启动我的应用程序后激活 VPN

 @Override
    protected void onStart() {
        super.onStart();
        if (isVpnConnectionActive()) {



        }else { jsonParse();}}

我请求通过这种方式解析json

    private void jsonParse() {

        final String url = "http://example.dns.net/json.php";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("employees");

                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject employee = jsonArray.getJSONObject(i);

                                String Urlx = employee.getString("urlx");
                                String Urlc = employee.getString("urlc");
                                String Urlb = employee.getString("urlb");
                                String TextBar = employee.getString("TextBar");


    DFG_1 = Urlx;
    DFG_2 = Urlc;
    DFG_3 = Urlb;
    Text_Bar = TextBar;
    text2.setText(TextBar);


}


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }


        });
        mQueue.add(request);

    }

为什么 android pie 和某些设备不能以这种方式工作?

【问题讨论】:

    标签: java android android-9.0-pie jsonparser android-vpn-service


    【解决方案1】:

    你有什么理由不使用NetworkCapabilities#hasTransport,见https://developer.android.com/reference/android/net/NetworkCapabilities#hasTransport(int)

    使用它,您可以使用TRANSPORT_VPN 检查“网络是否使用 VPN 传输”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-18
      • 2010-09-10
      相关资源
      最近更新 更多