我正在尝试确保在按下“后退”按钮或“主页”按钮时执行程序服务停止。
目前,我在onCreate
类的main activity
函数中包含此代码:
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
final Wifi wifiObject = new Wifi((WifiManager) getSystemService(Context.WIFI_SERVICE));
Runnable periodicTask = new Runnable() {
public void run() {
// For each AP in the database, we will fill the AP's ArrayList with their corresponding RSS values
for (Map.Entry<String, String> entry : AccessPoints.entrySet()) {
int APNoToBeSent = 0;
try {
wifiObject.scanWifi(entry.getKey(), accessPointMeanRSSArrayList, accessPointRSSFrequencyArrayList);
}
catch (Exception e) {
}
++APNoToBeSent;
}
System.out.println("Mean AP0 = " + accessPointMeanRSSArrayList.get(0));
System.out.println("Frqcy AP0 = " + accessPointRSSFrequencyArrayList.get(0));
}
};
executor.scheduleAtFixedRate(periodicTask, 0, 2, TimeUnit.SECONDS);
我一直在对此进行一些阅读,但我不确定这是应该在onStop()
还是onPause()
或完全不同的情况下发生?