简体   繁体   中英

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

I't trying to implement a local VpnService to have my app do some tasks, but I'm a little confused as to how to stop it one it started. The VpnService class and client activity are based on this repo: https://github.com/hexene/LocalVPN

The caller activity is basically this:

public class MainActivity extends AppCompatActivity {

private static final int VPN_REQUEST_CODE = 0x0F;
private boolean waitingForVPNStart;
private BroadcastReceiver vpnStateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (LocalVPNService.BROADCAST_VPN_STATE.equals(intent.getAction()))
            if (intent.getBooleanExtra("running", false))
                waitingForVPNStart = false;
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button vpnButton = (Button)findViewById(R.id.vpn);
    vpnButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startVPN();
        }
    });

    final Button vpnStopButton = (Button)findViewById(R.id.stopVpnButton);
    vpnStopButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            stopVPN();
        }
    });

    waitingForVPNStart = false;
    LocalBroadcastManager.getInstance(this).registerReceiver(vpnStateReceiver,
            new IntentFilter(LocalVPNService.BROADCAST_VPN_STATE));
}

private void startVPN() {
    Intent vpnIntent = VpnService.prepare(this);
    if (vpnIntent != null)
        startActivityForResult(vpnIntent, VPN_REQUEST_CODE); //Prepare to establish a VPN connection. This method returns null if the VPN application is already prepared or if the user has previously consented to the VPN application. Otherwise, it returns an Intent to a system activity.
    else
        onActivityResult(VPN_REQUEST_CODE, RESULT_OK, null);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == VPN_REQUEST_CODE && resultCode == RESULT_OK) {
        waitingForVPNStart = true;
        startService(new Intent(this, LocalVPNService.class));
        enableButton(false);
    }
}

What confuses me is: how would I call the service's onDestroy() method or something similar if I don't keep an instance if it in my main activity?

I looked at this answer and this and seen implementations of stopService, but I'm not sure how to handle the Intent, because it's not only used to call startService() but also involved in calling VpnService.prepare().

Edit: I tried stopService(new Intent(this, LocalVPNService.class)); but it doesn't stop it. I tried stopService(vpnIntent); and IT WORKS, but makes my app crash/close.

In your LocalVPNService class create a new broadcast:

private BroadcastReceiver stopBr = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
    if ("stop_kill".equals(intent.getAction())) {
         stopself();
    }
}
};

and in the onCreate method add this:

    LocalBroadcastManager lbm = 
    LocalBroadcastManager.getInstance(this);
    lbm.registerReceiver(stopBr, new IntentFilter("stop_kill"));

in your activity:

Intent intent = new Intent("stop_kill");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM