How to stop a service?

Asked

Viewed 115 times

1

I created a VPN connection in my app through a service, at the click of a button, the connection is made. But now I want to know how to do the reverse, IE, by clicking the button disable this connection. How can I do this?

Procedure to call the connection:

public void vpn() {
    Intent intent = android.net.VpnService.prepare(getApplicationContext());

    if (intent != null) {
        startActivityForResult(intent, 0);
    } else {
        onActivityResult(0, RESULT_OK, null);
    }
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        Intent intent = new Intent(this, DNSVpnService.class);
        startService(intent);
    }
}

Dnsvpnservice class:

public class DNSVpnService extends android.net.VpnService {

    private Thread mThread;
    private ParcelFileDescriptor mInterface;
    //a. Configure a builder for the interface.
    Builder builder = new Builder();

    // Services interface
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Start a new session by creating a new thread.
        mThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //a. Configure the TUN and get the interface.
                    mInterface = builder.setSession("OpenDNS")
                            .addAddress("192.168.0.1", 24)
                            .addDnsServer("208.67.222.222")
                            //.addRoute("0.0.0.0", 0)
                            .establish();
                    //b. Packets to be sent are queued in this input stream.
                    FileInputStream in = new FileInputStream(
                            mInterface.getFileDescriptor());
                    //b. Packets received need to be written to this output stream.
                    FileOutputStream out = new FileOutputStream(
                            mInterface.getFileDescriptor());
                    //c. The UDP channel can be used to pass/get ip package to/from server
                    DatagramChannel tunnel = DatagramChannel.open();
                    // Connect to the server, localhost is used for demonstration only.
                    tunnel.connect(new InetSocketAddress("127.0.0.1", 8087));
                    //d. Protect this socket, so package send by it will not be feedback to the vpn service.
                    protect(tunnel.socket());
                    //e. Use a loop to pass packets.
                    while (true) {
                        //get packet with in
                        //put packet to tunnel
                        //get packet form tunnel
                        //return packet with out
                        //sleep is a must
                        Thread.sleep(100);
                    }

                } catch (Exception e) {
                    // Catch any exception
                    e.printStackTrace();
                } finally {
                    try {
                        if (mInterface != null) {
                            mInterface.close();
                            mInterface = null;
                        }
                    } catch (Exception e) {

                    }
                }
            }

        }, "OpenDNS");

        //start the service
        mThread.start();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        if (mThread != null) {
            mThread.interrupt();
        }
        super.onDestroy();
    }
}

1 answer

3

To stop the service call the stopService() method by passing you an Intent equal to the one you used to start it

Intent intent = new Intent(this, DNSVpnService.class);
stopService(intent);
  • I already did that, the command runs but the VPN remains active...

  • 1

    You asked how to stop a service, it stops like this. Regarding the VPN, you probably have to do something on onDestroy() of the service.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.