我正在使用这种方法来获取广播地址数组。
旧 API 中不存在一些使用的方法和类所以我在方法的开头添加了版本检查。我试图在 Android 1.6 模拟器上运行它,但它失败并出现验证错误。
private InetAddress[] getBroadCast() throws SocketException
{
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion < 9)
{
try {
String ipString = getWifiIp();
int loc = ipString.lastIndexOf(".");
InetAddress baddrs[] = new InetAddress[1];
baddrs[0] = new InetAddress.getByName(ipString.substring(0, loc)+".255");
return baddrs;
} catch (UnknownHostException e) {
e.printStackTrace();
return null;
}
}
else{
String bcast="";
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (InterfaceAddress address : intf.getInterfaceAddresses()) {
if(address.getBroadcast()!=null) bcast=bcast+address.getBroadcast().toString().substring(1)+"::";
}}
String[] bcastarray= bcast.split("::");
InetAddress baddrs[] = new InetAddress[bcastarray.length];
for(int i=0;i<bcastarray.length;i++)
try {
baddrs[i] = new InetAddress.getByName(bcastarray[i]);
} catch (UnknownHostException e) {
e.printStackTrace();
}
return baddrs;
}
}
这些是出现的错误
10-12 11:38:22.030: W/dalvikvm(297): VFY: unable to resolve virtual method 4463: Ljava/net/NetworkInterface;.getInterfaceAddresses ()Ljava/util/List;
10-12 11:38:22.030: W/dalvikvm(297): VFY: rejecting opcode 0x6e at 0x0071
10-12 11:38:22.040: W/dalvikvm(297): VFY: rejected Lhello/BroadcastSend;.getBroadCast ()[Lhello/IPMAddress;
10-12 11:38:22.040: W/dalvikvm(297): Verifier rejected class Lhello/BroadcastSend;
10-12 11:38:22.040: W/dalvikvm(297): threadid=19: thread exiting with uncaught exception (group=0x4001aa28)
android.os.Build.VERSION.SDK_INT 仅适用于 API 级别 4 及更高版本。我正在寻找解决这些问题的方法