Android VPN 服务 Builder.establish() returns 空
Android VPN Service Builder.establish() returns null
我正在尝试让 VPN 服务在 android 上运行。
为此,我想我必须使用 VpnService class 中提供的 Builder class,它由 android SDK 提供。现在,当我尝试这样做时,方法
builder.establish()
returns 空。这是我写的完整方法;
// If theInterface still exists from last time, reuse it, it has the same parameters.
if (theInterface != null) {
return;
}
// configure the VPN settings.
Builder builder = new Builder();
builder.setMtu(1500);
builder.addAddress("10.0.2.0", 32);
builder.addRoute("0.0.0.0", 0);
try {
theInterface.close();
} catch (Exception e) {
// Ignore
}
theInterface = builder.establish();
Log.i(TAG + "IMPORTANT", builder.toString());
代码崩溃于:
Log.i(TAG + "IMPORTANT", builder.toString());
调试时,调试器会在整个程序执行过程中提示 'theInterface = null'。
编辑:
我查看了位于 android.net.VpnService
中的 Builder class
在这个class中,好像隐藏了一个class(IConnectivityManager),也许我的问题与这个有关...
这是我正在谈论的一段代码。
/**
* Use IConnectivityManager since those methods are hidden and not
* available in ConnectivityManager.
*/
private static IConnectivityManager getService() {
return IConnectivityManager.Stub.asInterface(
ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
}
作为VpnService.Builder.establish documents:
Returns ParcelFileDescriptor of the VPN interface, or null if the
application is not prepared.
需要先致电 VpnService.prepare: http://developer.android.com/reference/android/net/VpnService.html#prepare(android.content.Context)
我正在尝试让 VPN 服务在 android 上运行。
为此,我想我必须使用 VpnService class 中提供的 Builder class,它由 android SDK 提供。现在,当我尝试这样做时,方法
builder.establish()
returns 空。这是我写的完整方法;
// If theInterface still exists from last time, reuse it, it has the same parameters.
if (theInterface != null) {
return;
}
// configure the VPN settings.
Builder builder = new Builder();
builder.setMtu(1500);
builder.addAddress("10.0.2.0", 32);
builder.addRoute("0.0.0.0", 0);
try {
theInterface.close();
} catch (Exception e) {
// Ignore
}
theInterface = builder.establish();
Log.i(TAG + "IMPORTANT", builder.toString());
代码崩溃于:
Log.i(TAG + "IMPORTANT", builder.toString());
调试时,调试器会在整个程序执行过程中提示 'theInterface = null'。
编辑:
我查看了位于 android.net.VpnService
中的 Builder class
在这个class中,好像隐藏了一个class(IConnectivityManager),也许我的问题与这个有关...
这是我正在谈论的一段代码。
/**
* Use IConnectivityManager since those methods are hidden and not
* available in ConnectivityManager.
*/
private static IConnectivityManager getService() {
return IConnectivityManager.Stub.asInterface(
ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
}
作为VpnService.Builder.establish documents:
Returns ParcelFileDescriptor of the VPN interface, or null if the application is not prepared.
需要先致电 VpnService.prepare: http://developer.android.com/reference/android/net/VpnService.html#prepare(android.content.Context)