如何停止使用 prepare() 和 startService() 调用的 VpnService?

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

我没有尝试实现本地 VpnService 来让我的应用程序执行一些任务,但我对如何在它启动时停止它感到有点困惑。 VpnService class 和客户端 activity 基于此 repo: https://github.com/hexene/LocalVPN

来电者activity基本上是这样的:

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);
    }
}

让我感到困惑的是:如果我不在主实例中保留实例,我将如何调用服务的 onDestroy() 方法或类似方法 activity?

我查看了this answer and this并看到了stopService的实现,但我不确定如何处理Intent,因为它不仅用于调用startService()而且还参与调用VpnService.prepare().

编辑:我试过了 stopService(new Intent(this, LocalVPNService.class)); 但它并没有阻止它。我试过 stopService(vpnIntent); 并且成功了,但我的应用程序 crash/close.

在您的 LocalVPNService class 中创建一个新广播:

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

并在 onCreate 方法中添加:

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

在你的 activity:

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