Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get VPN Connection status on Android

Tags:

android

vpn

Is it possible to check if an Android device is connected to a VPN server? A search in the API provides 'paltform highlights' for Android 1.6, so that doesn't fill me with much confidence.

like image 489
Jonah H. Avatar asked Aug 11 '10 19:08

Jonah H.


People also ask

How do I know if my VPN is disconnected?

You can run the command "vpncli.exe" from the command prompt, this will tell you whether the VPN is connected or disconnected.

Is there a built in VPN on Android?

Android VPN optionsAndroid includes a built-in (PPTP, L2TP/IPSec, and IPSec) VPN client. Devices running Android 4.0 and later also support VPN apps. You might need a VPN app (instead of built-in VPN) for the following reasons: To configure the VPN using an enterprise mobility management (EMM) console.


1 Answers

You can register to broadcastreceiver and all vpn states will come to you application.

Add this to application manifest:

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

create a class:

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
    }
  }
}
like image 191
Nesim Razon Avatar answered Sep 28 '22 04:09

Nesim Razon