I'm trying to register receiver, that will check VPN status. I have tried this: Get VPN Connection status on Android but looks like it no longer works on ICS. I have checked android source code for some clue, but with no luck, only noticed that in ISC there is nothing like: vpn.connectivity
and connection_state
- it use to be in android 2.3. Also tried using android.net.conn.CONNECTIVITY_CHANGE
as my IntentFilter
, but it doesn't react on VPN connection at all (of course I have added permission android.permission.ACCESS_NETWORK_STATE
).
I thought that it is simple thing to do, but I already run out of ideas how to do it... Could someone help me with this please?
Asked
Active
Viewed 5,019 times
10
4 Answers
2
May be you can try to poll for DNS server changes. If it has changed then VPN has connected. However, I think there could be a lot of exception to this rule.

Community
- 1
- 1

Victor Ronin
- 22,758
- 18
- 92
- 184
0
My solution is polling a NIC list.
# You can get the list by executing command "ls /sys/class/net".
If the list has "tun0", the device already has connected to VPN.
0
NetworkCapabilities
worked for me in API 21+. Unfortunately I haven't found a solution for 19-20. You have to loop over all existing networks and check which has VPN_TRANSPORT
ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
Network[] networks = cm.getAllNetworks();
Log.i(TAG, "Network count: " + networks.length);
for(int i = 0; i < networks.length; i++) {
NetworkCapabilities caps = cm.getNetworkCapabilities(networks[i]);
Log.i(TAG, "Network " + i + ": " + networks[i].toString());
Log.i(TAG, "VPN transport is: " + caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN));
Log.i(TAG, "NOT_VPN capability is: " + caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN));
}

Isaac Madwed
- 986
- 9
- 23
0
for (Enumeration<NetworkInterface> en =
NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
if (intf.getName().contains("tun0") || intf.getName().contains("ppp0")) {
vpnInterface = intf;
break;
}
}
For the VPNNetwork Interface
for (Enumeration<InetAddress> en =
vpnInterface.getInetAddresses(); en.hasMoreElements(); ) {
InetAddress address = en.nextElement();
if (!address.isLoopbackAddress()) {
String ip = address.getHostAddress();
break;
}
}
and the InetAddress
Thats everything I know by the moment now
To check if it's up etc. maybe
if (vpnInterface.isUp())
I do have implemented a code which calls itself after a serveral time and send a message to the ApplicationHandlers

Pwnstar
- 2,333
- 2
- 29
- 52