0

我遇到了一些设备的问题,比如一些 android 盒子或华为手机如果用户使用数据包监控应用程序我需要保护我的应用程序我所做的是如果用户在他们的设备上激活了 vpn 不请求 jsonparse 但有些设备即使vpn 未激活 jsonparse not run 。

这是我的代码

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 和某些设备不能以这种方式工作?

4

1 回答 1

0

您是否有理由不使用NetworkCapabilities#hasTransport,请参阅https://developer.android.com/reference/android/net/NetworkCapabilities#hasTransport(int)

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

于 2019-06-27T13:44:44.307 回答