简体   繁体   English

检查VPN状态是否已连接或未连接

[英]Check VPN Status Connected or Not Connected

I'm making an app which wants to detect VPN Connection. 我正在制作一个想要检测VPN连接的应用程序。 My Question is I want to Detect VPN Connection, If VPN is Connected I don't want to allow the user to use my app, In case VPN is off I want to allow the user to use my app. 我的问题是我想检测VPN连接,如果VPN已连接我不想让用户使用我的应用程序,如果VPN关闭我想让用户使用我的应用程序。

Before Asking This question I read this Documents VPNService VPNStatus 在询问之前我已经阅读了这份文件VPNService VPNStatus

But I can't get Solution. 但我无法得到解决方案。 If anyone knows the solution, Please Share with me. 如果有人知道解决方案,请与我分享。

Thanks in Advance 提前致谢

This is works for me: Tested from API 16 to 23: 这对我有用:从API 16到23进行测试:

 List<String> networkList = new ArrayList<>();
try {
    for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
        if (networkInterface.isUp())
            networkList.add(networkInterface.getName());
    }
} catch (Exception ex) {
    Timber.d("isVpnUsing Network List didn't received");
}

return networkList.contains("tun0");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM