我正在编写一个安卓应用程序,遇到两个问题。
我从/proc/net/arp
读取本地ARP表,并将IP地址和相应的MAC地址保存在哈希映射中。请看我的函数。它可以正常工作。
/**
* Extract and save ip and corresponding MAC address from arp table in HashMap
*/
public Map<String, String> createArpMap() throws IOException {
checkMapARP.clear();
BufferedReader localBufferdReader = new BufferedReader(new FileReader(new File("/proc/net/arp")));
String line = "";
while ((line = localBufferdReader.readLine()) != null) {
String[] ipmac = line.split("[ ]+");
if (!ipmac[0].matches("IP")) {
String ip = ipmac[0];
String mac = ipmac[3];
if (!checkMapARP.containsKey(ip)) {
checkMapARP.put(ip, mac);
}
}
}
return Collections.unmodifiableMap(checkMapARP);
}
第一个问题:
我也在使用广播接收器。当我的应用程序接收到状态
WifiManager.NETWORK_STATE_CHANGED_ACTION
时,我会检查是否已经建立了与网关的连接。如果是,则调用我的函数读取arp表。但此时系统尚未建立arp表。有时当我接收到连接状态时,arp表仍为空。有没有人有解决这个问题的想法?
第二个问题:
我想以持久化的方式保存网关的IP和MAC地址。现在我正在使用共享首选项来实现这一点。也许将其写入内部存储器会更好?
有什么提示吗?