【问题标题】:On Android app cold start, prevent network requests until VPN started在 Android 应用冷启动时,阻止网络请求,直到 VPN 启动
【发布时间】:2020-05-22 02:44:12
【问题描述】:

在我们的应用中,我们有许多不同的模块可以自行发出网络请求。应用程序可以选择使用 VPN 进行安全连接。 VPN 服务仅为我们的应用程序处理网络。 我的问题是:在应用程序冷启动时,启用 VPN 功能时,如何防止模块在 VPN 服务启动之前发出网络请求。

【问题讨论】:

    标签: android android-vpn-service


    【解决方案1】:

    我的项目中有类似的问题,但是我有一个模块需要等待建立 VPN。无论如何,我有一个小代码来检测是否连接了 VPN。

    代码如下:

    class VpnMonitor {
        private final String VPN_INTERFACE = "tun0";
    
        private OnVpnStatusChange mCallback;
        private MonitorThread mThread = null;
        private Handler mHandler = new Handler(Looper.getMainLooper());
    
        public interface OnVpnStatusChange {
            void onVpnConnect();
    
            void onVpnDisconnect();
        }
    
        VpnMonitor() {
            this(null);
        }
    
        VpnMonitor(@Nullable OnVpnStatusChange callback) {
            mCallback = callback;
        }
    
        void startMonitor() {
            if ((mThread == null) || !mThread.isAlive()) {
                mThread = new MonitorThread();
                mThread.start();
            }
        }
    
        void stopMonitor() {
            if (mThread != null) {
                mThread.terminate();
                try {
                    mThread.join();
                } catch (Exception e) {
                }
                mThread = null;
            }
        }
    
        boolean isVpnConnectionUp() {
            try {
                ArrayList<NetworkInterface> infs = Collections.list(NetworkInterface.getNetworkInterfaces());
                for (NetworkInterface inf : infs) {
                    if (inf.getName().equals(VPN_INTERFACE) && inf.isUp() && inf.getInterfaceAddresses().size() > 0) {
                        return true;
                    }
                }
            } catch (SocketException e) {
                return false;
            }
    
            return false;
        }
    
        private class MonitorThread extends Thread {
            private volatile boolean mRunning = true;
    
            void terminate() {
                mRunning = false;
            }
    
            @Override
            public void run() {
                mRunning = true;
    
                for (int i = 0; i < 5; i++) {
                    try {
                        delay(100);
    
                        ArrayList<NetworkInterface> infs = Collections.list(NetworkInterface.getNetworkInterfaces());
    
                        for (NetworkInterface inf : infs) {
                            if (inf.getName().equals(VPN_INTERFACE)) {
                                for (int r = 0; r < 10; r++) {
                                    if (!mRunning) {
                                        mHandler.post(new Runnable() {
                                            @Override
                                            public void run() {
                                                if (mCallback != null) {
                                                    mCallback.onVpnDisconnect();
                                                }
                                            }
                                        });
    
                                        return;
                                    }
    
                                    if (inf.isUp() && inf.getInterfaceAddresses().size() > 0) {
                                        delay(1000);
    
                                        mHandler.post(new Runnable() {
                                            @Override
                                            public void run() {
                                                if (mCallback != null) {
                                                    mCallback.onVpnConnect();
                                                }
                                            }
                                        });
    
                                        return;
                                    }
    
                                    delay(50);
                                }
                            }
                        }
                    } catch (SocketException e) {
                    }
                }
    
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (mCallback != null) {
                            mCallback.onVpnDisconnect();
                        }
                    }
                });
    
            }
    
            private void delay(int time) {
                try {
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        }
    }
    

    也许它也可以帮助你。随意更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-20
      • 2013-05-25
      • 1970-01-01
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多