【问题标题】:DhcpInfo isn't responding the netmask in Android LollipopDhcpInfo 没有响应 Android Lollipop 中的网络掩码
【发布时间】:2015-01-12 00:40:03
【问题描述】:

昨天我将我的 nexus 5 更新为棒棒糖并且我的应用程序停止工作,经过一番调查后我发现 DhcpInfo 不是在网络掩码变量上返回 null 的问题。

我找不到这个类的任何替代品。

【问题讨论】:

  • 我遇到了同样的问题,还没有找到解决办法。但是,您从哪里了解到 DhcpInfo 已被弃用?
  • 似乎 DhcpInfo 不再被弃用,但我的应用程序配置为 API 18,在 API 18 中它曾经被弃用:) developer.android.com/sdk/api_diff/19/changes/…
  • 您使用的是安卓棒棒糖。所以你应该使用 Android API 级别 21 :)
  • 这似乎是棒棒糖的错误。我刚买了一台装有 Android 5.0 的 HTC nexus 9,但我的应用程序无法在它上面运行。检查代码后,DhcpInfo 的网络掩码总是返回 0。
  • 这是 Android 5 中的这个错误:code.google.com/p/android/issues/detail?id=82477

标签: android android-5.0-lollipop netmask


【解决方案1】:

你可以使用InterfaceAddressgetNetworkPrefixLength方法,你可以从NetworkInterface得到。它在 Lollipop 中返回正确的值。

NetworkInterface networkInterface = NetworkInterface.getByInetAddress(ipAddress);
for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
    short netPrefix = address.getNetworkPrefixLength());
}

注意:它返回网络前缀长度,因此您必须对其进行转换(/24 表示 255.255.255.0 等)

【讨论】:

  • 这会返回 wifi 前缀还是 WAN 前缀?
猜你喜欢
  • 2019-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多