在 Android 上以编程方式设置 vpn

use*_*428 6 vpn android android-vpn-service

我找到了以下代码以编程方式建立一个新的 vpn,但我不知道如何使用它来创建我的应用程序

VpnService service = context.getSystemService(VPN_SERVICE);
VpnProfile profile = VpnProfile.create(L2TP_PROFILE);
profile.setName(myServerName);
profile.setServerName(myServerAddress);
profile.setRouteList("192.168.1.0/255.255.255.0,192.168.10.0/255.255.255.0");
service.connect(profile, "myUserName", "myPassword");
service.setNotificationIntent(myIntent);
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我提供示例代码吗?甚至有可能实现dis吗?

bra*_*ipt 2

注意:此答案已过时,现在可能不准确。

看看这个问题:如何以编程方式配置 VPN?

虽然你的不一定是重复的,但答案可能是相同的,因为你将需要公开隐藏的 API(如果可用)或更糟糕的是,依赖于被 root 的设备。

可以理解的是,以编程方式创建 VPN 连接会给最终用户带来固有的安全风险,不应在未经考虑的情况下实施。