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 is on.
Asked
Active
Viewed 2,966 times
1 Answers
2
Check this:
public static boolean isVpnConnectionActive(){
List<String> networks = new ArrayList<>();
try {
for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
if (networkInterface.isUp()){
networks.add(networkInterface.getName());
}
}
} catch (Exception ignored) {
}
return networks.contains("tun") || networks.contains("ppp") || networks.contains("pptp");
}

Hamed
- 5,867
- 4
- 32
- 56
-
how can i sure about "tun0"? can it be "tun1 or 2 or 3" ? – Wasi Sadman Jun 24 '21 at 17:59
-
@WasiSadman no, it is always `tun0`, but you can use `tun` instead of `tun0` to check all possible conditions. I will update the answer. – Hamed Jun 25 '21 at 03:15
-
well i found for my app **tun1** – Wasi Sadman Jun 25 '21 at 14:02