7
votes

Is there a way to get notified when a VPN is connected or disconnected? Because of what I see, the ConnectivityManager does not broadcast any intent about that.

I also tried (unsuccessfully) to register to the hidden ACTION_VPN_CONNECTIVITY broadcast (as seen in android.net.vpn.VpnManager.java source code):

    context.registerReceiver(new BroadcastReceiver()
        {
            public void onReceive(Context context, Intent intent)
            {
                android.util.Log.d("MyApp", "Received VPN broadcast.");
            }
        }, new IntentFilter("vpn.connectivity")); // VpnManager.ACTION_VPN_CONNECTIVITY

So is there a way to detect if a VPN is connected, beside polling the networks interfaces periodically to detect the creation of a new network interface (usually ppp0)?

Best regards, David

3
A quick update to say that the "vpn.connectivity" is no more present in Android 4.0 (ICS).dbernard

3 Answers

1
votes

sgs2 and emulator worked with:

    getApplicationContext().registerReceiver(new BroadcastReceiver()
    {
        public void onReceive(Context context, Intent intent)
        {
            logger.Log("VPN broadcast "+context.toString()+": ["+intent.toString()+"]");
        }
    }, new IntentFilter("vpn.connectivity"));
0
votes

I tried listening for the "vpn.connectivity" event you mentioned, and it did work on my Nexus One (Android 2.3.3). I don't know if it makes any difference, but I registered the receiver in the manifest instead of doing it at runtime.

0
votes

You should use this exemple :

 <service android:name=".ExampleVpnService"
     android:permission="android.permission.BIND_VPN_SERVICE">
 <intent-filter>
     <action android:name="android.net.VpnService"/>
 </intent-filter>