Automatically open a VPN connection

On Android, we would like to use a VPN when a user is connected to a cellular network.

But the problem is that the VPN connection does not open automatically when switching between WiFi and the cellular network.

It also does not automatically connect when the device restarts.

Is there any potential way to automatically open a VPN connection when switching between WiFi and cellular network, as well as when rebooting the device.

We need this for Android Gingerbread and Ice Cream Sandwich.

A VPN opens when users switch between WiFi and a cellular network.

+4
source share
1 answer
  • When changing the network:

add the permission <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> to your manifest. Configure a BroadcastReceiver that listens for the action android.net.conn.CONNECTIVITY_CHANGE . In this receiver, get an instance of ConnectivityManager using Context.getSystemService(Context.CONNECTIVITY_SERVICE); and check if the network is connected. If so, you can continue your vpn login.

  • On reboot:

add permission <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> to the manifest. Configure a BroadcastReceiver that listens for the Intent.ACTION_BOOT_COMPLETED action. Here you can proceed to establish your vpn connection.

+2
source

All Articles