用于计算扫描开始和结束时间的代码。一旦扫描开始就调用scanTime,并在收到结果后立即调用retTime,但是,获得两个retTime并且scanTime和retTime之间的差异不一致
public void startService() {
br = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent intent) {
if (location != null) {
retTime = System.currentTimeMillis() / 1000L;
Log.i("end", Long.toString(retTime));
sendResults(wifi.getScanResults(), androidID, Long.toString(retTime), location);
Long result = retTime - scanTime;
} else {
Log.i("Location", "is Missing");
}
}
};
context.registerReceiver(br, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
t = new Thread(new Runnable() {
public void run() {
try {
setSleepTime(dataTimeDifference);
while (!Thread.interrupted()) {
wifi.startScan();
scanTime = System.currentTimeMillis() / 1000L;
Log.i("start", Long.toString(scanTime));
Thread.sleep(sleepingTime);
}
} catch (InterruptedException e) {
}
}
});
t.start();
}
答案 0 :(得分:0)
简而言之,您知道触发BroadcastReceiver时扫描已完成。
这可能无法直接回答您的问题,但似乎这可能是实现您想要的功能的更好方法。 不要在Runnable中使用Thread.sleep()和while循环,只需依赖BroadcastReceiver来确定何时开始新的扫描。
另请注意,用户和操作系统都可以启动扫描,并且当扫描完成时也会触发BroadcastReceiver。
private final Runnable mStartScan = new Runnable() {
@Override
public void run() {
wifi.startScan();
}
};
public void startService() {
br = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent intent) {
String action = intent.getAction();
if (action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)){
if (location != null) {
sendResults(wifi.getScanResults(), androidID, Long.toString(retTime), location);
} else {
Log.i("Location", "is Missing");
}
t = new Thread(mStartScan);
t.start();
}
}
};
context.registerReceiver(br, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
t = new Thread(mStartScan);
t.start();
}