首页 > 解决方案 > 多个处理程序触发器

问题描述

我的应用程序使用手机 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);
    }
};

标签: handler

解决方案


推荐阅读