【发布时间】:2012-06-05 20:50:46
【问题描述】:
我正在以编程方式在 Android 设备上设置 VPN 连接。对于使用 OS 2.3.5 及之前的设备,我成功地做到了这一点(我使用反射来访问隐藏类)。但是在 android 4.0 中,他们摆脱了旧的类并改用 VPNService 类。
我认为最好的起点是使用 Android 提供的 ToyVPN 示例,但我面临着很多挑战。在示例代码中,他们只需要发送服务器地址:
InetSocketAddress server = new InetSocketAddress(mServerAddress, Integer.parseInt(mServerPort));
然后通过打开通道创建VPN隧道:
tunnel = DatagramChannel.open();
但就我而言,我需要发送服务器地址、用户名和密码。到目前为止,我还没有弄清楚如何做到这一点。我最好的猜测是做这样的事情:
Authenticator.setDefault(new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user","pass".toCharArray());
}});
try {
// Create a DatagramChannel as the VPN tunnel.
tunnel = DatagramChannel.open();
但这不起作用。 所以我要问的是:
- 除了在 ToyVpn 中使用的方法之外,还有其他方法可以以编程方式创建 VPN 连接吗?
- 如果没有,当我想与服务器建立连接时如何发送凭据?
编辑
我忘了提到我需要指定 VPN 类型(PPTP、L2TP、L2TP/IPSec PSK 或 L2TP/IPSec CRT)。
【问题讨论】:
-
我也在寻找这些问题的答案,能否分享一下你的发现
-
不,遗憾的是我从未找到解决方案。
-
我也对解决方案感兴趣
-
@Ahmad,你找到解决方案了吗?我也需要解决方案
-
@kh_4 你有什么解决办法吗?
标签: android android-4.0-ice-cream-sandwich vpn