我正在实施跟踪服务,以跟踪我所在城市的公交车位置。巴士配备WIFI access point (AP)
。因此,在从JSON文本文档解析后,我将根据WIFI AP的SSID检测路由号。我已经实现了一个BroadcastReceiver来完成这项工作,但是要避免总线靠近用户一段时间并且用户没有进入总线的情况(可能是因为总线不是用户正在等待的正确总线)想要在60秒后将路由发送到服务器。因此,应用程序必须首次捕获SSID,然后在60秒后检查它是否仍在范围内,然后在此情况下将路由发送到服务器。我怎么能这样呢?
我感谢任何帮助。
JSON文档的一部分:
{
"wifi": [
{
"ssid": "Fr Privat",
"route_number": 1
},
{
"ssid": "Fr WLAN Hotspot+",
"route_number": 4
}
]
}
TrackingService类:
public class TrackingService extends Service implements
LocationListener {
private int route_number = 0;
private final BroadcastReceiver wifi_receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled()) {
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
final List<ScanResult> results = wifiManager.getScanResults();
if (results != null) {
// list of access points from the last scan
List<ScanResult> updatedResults = new ArrayList<ScanResult>();
// pick Wi-Fi access points which begins with these "Fr"
// characters.
for (int i = 0; i < results.size(); i++) {
String ssid = results.get(i).SSID;
if (ssid.startsWith("Fr")) {
updatedResults.add(results.get(i));
}
}
if (updatedResults.size() > 0 && route_number == 0) {
String ssid = deliverBestAccessPoint(updatedResults);
// retrieve data from json String and parse it.
route_number = parse_SSID(ssid);
}
}
} else {
Toast.makeText(context, "Please turn your Wi-Fi on",
Toast.LENGTH_SHORT).show();
}
}
private String deliverBestAccessPoint(final List<ScanResult> updatedResults) {
ScanResult bestSignal = (ScanResult) updatedResults.get(0);
for (int i = 1; i < updatedResults.size(); i++) {
ScanResult result = (ScanResult) updatedResults.get(i);
bestSignal = (WifiManager.compareSignalLevel(bestSignal.level,
result.level) < 0) ? result : bestSignal;
}
return bestSignal.SSID;
}
};
@Override
public void onLocationChanged(Location location) {
if (location != null) {
......
String jSONString = convertToJSON(mac, pLong, pLat, time, speed);
if (isNetworkAvailable()) {
PostData sender = new PostData(TrackingService.this);
sender.post_data(jSONString, this);
}
}
}
}
答案 0 :(得分:0)
您可以使用Handler类将发送到服务器的SSID延迟60秒。
每次获得SSID时,您都会使用自定义postDelayed()调用Runnable函数,并延迟60秒。
在Runnable中,检查SSID是否在范围内,如果它在范围内,则将SSID发送到服务器。
希望这有帮助