Get VPN Connection status on Android
Asked Answered
G

1

10

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.

Galvano answered 11/8, 2010 at 19:16 Comment(2)
Can anyone throw some light on the above query. I also need to get VPN status . But i find out the APIs are hidden . Any suggestions how can we access those APIsAdora
this previous answer looks like what you need: https://mcmap.net/q/876418/-how-to-find-the-status-of-vpn-connection-through-framework-apis-or-any-other-efficient-methodAtomizer
H
9

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
    }
  }
}
Headed answered 12/4, 2012 at 18:19 Comment(4)
I don't need this anymore, but thanks for the answer! I don't have the time to test it, but I trust you.Galvano
the solution failed to work on Android 4.0+, I read the Android source, there's no such vpn.connectivity broadcast anymore...Acidity
what is the latest solution then?Precarious
bump! Update for 2022?Kellsie

© 2022 - 2024 — McMap. All rights reserved.