在Android上获取VPN连接状态

是否有可能检查一个Android设备是否连接到VPN服务器? 在API中search提供了Android 1.6的“平台亮点”,所以这不会让我充满信心。

Solutions Collecting From Web of "在Android上获取VPN连接状态"

您可以注册到broadcastreceiver和所有vpn状态将来到您的应用程序。

将此添加到应用程序清单:

<receiver android:name=".ConnectivityReceiver"> <intent-filter> <action android:name="vpn.connectivity" /> </intent-filter> </receiver> 

创build一个类:

 import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class ConnectivityReceiver extends BroadcastReceiver { public void onReceive(Context c, Intent intent) { String state = intent.getSerializableExtra("connection_state").toString(); Log.d("**************", state.toString()); if (state.equals("CONNECTING")) { // Do what needs to be done } else if (state.equals("CONNECTED")) { // Do what needs to be done } else if (state.equals("IDLE")) { int errorCode = intent.getIntExtra("err", 0); if (errorCode != 0) { // Do what needs to be done to report a failure } else { // Normal disconnect } } else if (state.equals("DISCONNECTING")) { // Usually not very interesting } } }