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

Dmi*_*ryB 2 java service android android-intent android-vpn-service

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

调用者活动基本上是这样的:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

我查看了this answerthis并看到了stopService的实现,但我不确定如何处理Intent,因为它不仅用于调用startService(),还涉及调用VpnService.prepare()。

编辑:我试过了, stopService(new Intent(this, LocalVPNService.class));但它并没有阻止它。我试过了stopService(vpnIntent);,它有效,但使我的应用程序崩溃/关闭。

小智 6

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

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

并在onCreate方法中添加:

    LocalBroadcastManager lbm = 
    LocalBroadcastManager.getInstance(this);
    lbm.registerReceiver(stopBr, new IntentFilter("stop_kill"));
Run Code Online (Sandbox Code Playgroud)

在您的活动中:

Intent intent = new Intent("stop_kill");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
Run Code Online (Sandbox Code Playgroud)