Android VpnService - How to check VpnService if it was started?

liyustar

I have two applications that uses VpnService class. But there can be only one VPN connection running at the same time. The existing interface is deactivated when a new one is created and I want the new application not to start vpnservice to avoid old one getting deactivated and its VPN connection. So I want to check if another vpnservice was started before invoke startService().

Is there any api to do this ?

Rakesh patanga

As far as I know you can not check directly.The below is the approach that I use in my application for checking VPN connection.

1)Make a HTTPGet call for http://jsonip.com

                    public static String GetDeviceIp(){

                    HttpGet req = new HttpGet("http://jsonip.com");

                    DefaultHttpClient httpClient = new DefaultHttpClient();

                    HttpResponse response = httpClient.execute(req); 


                    if (response.getStatusLine().getStatusCode() == 200){
                        ipaddress=parseJson(response.getEntity().getContent());

                    }else
                    { ipaddress ="EMPTY" }

                    return ipaddress
          }

2)Parse the json response and extract the Ip address from response

      public static String parseJson(InputStream in)
      {
         String iP;
        try{

        String result;
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = bufferedReader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        result = sb.toString(); 

        JSONObject jsonObject = new JSONObject(result);
        iP=jsonObject.getString("ip");
    }catch (Exception e){
        iP="EMPTY";
    }

    return iP;
   }

3)Compare if VPN server Ip and extracted Ip are equal if so then the VPN is on else VPN is off

public static boolean IsVPNON(){
    return GetDeviceIp().equals("VPN-IP address");}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Android VpnService protect socket that's stored in native code?

Android firewall with VpnService

How to check if python unit test started in PyCharm or not?

A firewall for Android with VpnService. Responses are delivered, but a SocketTimeoutException is thrown

How to check if game was started once

how to check if a user started a bot in telegram or not?

How to check if a WildFly Server has started successfully using command/script?

what is Android VpnService.Builder setBlocking

Xamarin Android - VpnService is blocking all apps

How to stop a VpnService that was called with prepare() and startService()?

Android Management API and VpnService

How to check if webview YouTube video player was started

How to check inside Ktor that the Netty is actually started?

How to check if a process started in the background still running?

VpnService always-on "not supported by this app"

Android VpnService start on Device ACTION_BOOT_COMPLETED not working

Android VpnService to capture packets won't capture packets

Android VpnService 'protect' method not working

Check how a script was started

How to start a Service when the Android application is started?

Android VpnService with multiple addresses and routes?

How to check if application has started inside container

Find how Android App was started in Application class

How to check that Zookeeper server is started using Curator?

Issue about Android understanding VpnService code

Python how to check if a thread has already started

How to check if a new process started in windows?

VpnService.Builder IPv6 error null object

How to check whether tomcat server is started up