【问题标题】:Android VPN Service Builder.establish() returns nullAndroid VPN Service Builder.establish() 返回 null
【发布时间】:2015-05-27 02:48:19
【问题描述】:

我正在尝试使 VPN 服务在 android 上运行。
为此,我想我必须使用android SDK提供的VpnService类中提供的Builder类。现在当我尝试这样做时,方法

builder.establish()

返回空值。这是我写的完整方法;

// 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 类 在这个类中,好像有一个隐藏类(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));
}

【问题讨论】:

  • "调试器在程序的整个执行过程中都说'theInterface = null'。"你确定它不会在theInterface.close(); 崩溃吗?
  • 感谢您的回复,我确信这不会影响结果,我只是尝试删除那段代码并再次运行它,结果相同。
  • 我看到了同样的问题,但是当我重新启动我的设备 (LG G3) 时,VPN 不可用,仍在寻找解决方案
  • 感谢您的评论,我真的很希望有人能解决这个问题,因为我还没有...

标签: java android vpn


【解决方案1】:

作为 VpnService.Builder.建立documents:

返回 VPN 接口的 ParcelFileDescriptor,如果 申请未准备好。

需要先调用 VpnService.prepare:http://developer.android.com/reference/android/net/VpnService.html#prepare(android.content.Context)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-22
    • 2012-05-28
    • 2014-12-05
    • 2021-07-07
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多