handler - 多个处理程序触发器
问题描述
我的应用程序使用手机 GPS 记录字段元素以帮助地图绘制者设计定向地图。航点或航迹以 .gpx 格式保存,记录速度可以在 2 到 6 秒@Preference 之间调整。
通过使用后延迟处理程序将电话的时间窗口与卫星时间窗口进行比较来执行检查。每 8 次记录间隔检查一次卫星信号。它仅在 onResume 上启动并要求在 onPause 上停止。在启动它之前,有一个调用来杀死任何处理程序。处理程序还等待 30 秒以稳定信号。
由于未确定的原因,有时不止一个处理程序同时处于活动状态,这导致信号测试失败,因为卫星时间值变得小于电话的时间值。如果我转到首选项并在小于时间窗口(8 X 记录间隔)内返回主应用程序,我可以强制问题(有时)出现。
Toast 消息和 r 仅用于调试目的。
我做错了什么?
@Override
protected void onResume() {
super.onResume();
// ...
//
//Set the timers that will supervise the GPS signal activity
/*
* Compare timers to see if the satellites still visible within a time window
*/
Date currentTime = Calendar.getInstance().getTime();
mPhoneActualTime = currentTime.getTime();
mPhonePreviousTime = mPhoneActualTime;
Toast.makeText(Oribooklet.this,"Kill - RESUME", Toast.LENGTH_SHORT).show();
handler.removeCallbacksAndMessages(null);
checkGPS();
}
//
public void onPause() {
super.onPause();
Toast.makeText(Oribooklet.this,"Kill - PAUSE", Toast.LENGTH_SHORT).show();
handler.removeCallbacksAndMessages(null);
}
//
// A Handler is responsible to check if we loose the satellites
// Start 30 seconds after GPS is on to allow stabilization.
// Check every 8 GPS data update to not overload
public void checkGPS() {
Handler handler = new Handler();
Toast.makeText(Oribooklet.this, "Trigger", Toast.LENGTH_SHORT).show();
handler.postDelayed(checkGPSSignalLoss, 30 * 1000);
}
private ProgressBar progressBar;
private int r;
private Runnable checkGPSSignalLoss = new Runnable() {
@Override
public void run() {
progressBar = (ProgressBar) findViewById(R.id.progress);
progressBar.setVisibility(View.INVISIBLE);
Date currentTime = Calendar.getInstance().getTime();
mPhoneActualTime = currentTime.getTime();
// Use 70% as a GPS communication error of real time
long phoneTimeDifference = (long) (0.7 * (mPhoneActualTime - mPhonePreviousTime));
long gpsTimeDifference = mGPSActualTime - mGPSPreviousTime;
mPhonePreviousTime = mPhoneActualTime;
mGPSPreviousTime = mGPSActualTime;
++r;
Toast.makeText(Oribooklet.this, gpsTimeDifference + " " +
phoneTimeDifference + " - " +
String.valueOf(r)
, Toast.LENGTH_SHORT).show();
// Check the time difference between the 2 GPS recordings above
// It must be at least 10% higher than real time, if not, the satellites are gone!
if (gpsTimeDifference < phoneTimeDifference ){
// Change UI
String textGPS = getResources().getString(R.string.waiting);
TextView textOut = (TextView) findViewById(R.id.textSatellites);
// Set a turning progress bar and a message to feedback the loss
progressBar = (ProgressBar) findViewById(R.id.progress);
textOut.setText(textGPS);
progressBar.setVisibility(View.VISIBLE);
}
// Check every 8X the GPS communication interval
handler.postDelayed(this, lTimerToUpdateGPS);
}
};
解决方案
推荐阅读
- r - 如何将时间序列图添加到常规图?
- javascript - 如何在箭头函数表达式中编写 if 语句?
- javascript - 等待元素可见的功能不起作用javascript
- java - 为什么 Java 在尝试打印大随机数时会报告不匹配?
- java - Java Swing:使用 Thread.sleep() 制作的动画未按预期工作
- php - 错误的表格格式引导程序
- python-3.x - python:tkinter treeview 颜色没有更新
- java - 尝试在 Spring Boot 中访问两个数据库时创建名为“entityManagerFactory”的 bean 时出错
- javascript - 如何使用基于另一个列表框的选定项目更改列表框内容的java将javascript代码添加到pdf
- python - 如何使用 PyQt5 保存任何文件?