2

我没有尝试实现本地 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);
    }
}

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

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

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

4

1 回答 1

6

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

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

在您的活动中:

Intent intent = new Intent("stop_kill");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
于 2018-03-18T15:29:35.787 回答