我的应用程序记录了字段元素,以帮助制图人员使用手机GPS设计定向越野赛地图。使用.gpx格式保存Waypoint或Trackway,并且可以将“ Preference”从2秒调整为6秒。
通过使用后延迟处理程序将电话的时间窗口与卫星时间窗口进行比较来执行检查。记录间隔每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);
}
};