本文介绍了检查VPN连接在Android中是否处于活动状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在没有root权限的Android 4.4设备上有一个第三方VPN应用程序,并且想编写一个后台服务来监视VPN连接并在VPN连接断开时提醒用户.
I have a third party VPN app on my non-rooted Android 4.4 device, and want to write a background service to monitor the VPN connection and alert the user if the VPN connection has been broken.
有没有办法做到这一点?我找不到使用VPNService API的任何方式.
Is there a way to do this? I couldn't find any way using the VPNService API.
感谢-D
推荐答案
这对我有用:从API 16到23进行了测试:
This is works for me:Tested from API 16 to 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");
P.S.也可以是另一个名为"ppp0"的networkInterface,但是在我对不同VPN应用程序的测试中,它始终是"tun0"
P.S. Also it can be another networkInterface with name "ppp0", but in my tests with different VPN apps it's always was "tun0"
这篇关于检查VPN连接在Android中是否处于活动状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!