No connectivity change with VPN?

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


sgs2和模拟器一起工作:

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

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.


你应该使用这个例子:

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

链接地址: http://www.djcxy.com/p/63374.html

上一篇: 在Android上发送短信

下一篇: VPN没有连接变化?