gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:20:44 35 4
gpt4 key购买 nike

我并没有尝试实现一个本地 VpnService 来让我的应用程序执行一些任务,但我对如何在它启动时停止它感到有点困惑。 VpnService 类和客户端 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);
}
}

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

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

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

最佳答案

在您的 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"));

在你的 Activity 中:

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

关于java - 如何停止使用 prepare() 和 startService() 调用的 VpnService?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49349167/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com