设置为在计时器期间运行的代码被间歇性触发

时间:2018-12-11 02:54:26

标签: android countdowntimer android-location android-gps android-timer

我有一个倒计时计时器,当用户按下按钮时会运行,在计时器结束时,设备的位置将发送到用户为其输入电话号码的另一设备。这是我的计时器代码::

private void initTimer() {

    if(timer==null)
    {

        long totalTime= (PersistData.getIntData(con, AppConstant.selectedMinute)*60*1000)+(PersistData.getIntData(con, AppConstant.selectedseconds)*1000);
        timer=new CountDownTimer(totalTime, 1000) {

            public void onTick(long millisUntilFinished) {


                timerView.setText(getFormatterTimerText(millisUntilFinished));

                if(millisUntilFinished<60*1000)
                {

                    continueFlashAndAudio();

                     /*
                refresh GPS Location again
                 */

                    updateGPSLocation (millisUntilFinished);
                }



            }

            public void onFinish() {

                showFinishView();
            }
        };

    }
}

计时器运行时,应该运行“ updategpslocation”功能:

private void updateGPSLocation(long millisUntilFinished) {

    Log.e("current time remaining", " time: "+millisUntilFinished/1000);


    if(millisUntilFinished/1000==30)

    {

        Log.e("30 seconds remaining", "get locaiton");

        getLocation();
    }

    if(millisUntilFinished/1000==10)

    {

        Log.e("10 seconds remaining", "get locaiton");

        getLocation();
    }
}

我遇到的问题是,大约有一半的时间,该位置将不会更新,并且会将先前提取到的位置发送到另一部手机。

这是我获取位置的代码:

private void getLocation()
{

    GPSTracker tracker=new GPSTracker(con);
    if(tracker.canGetLocation())
    {
        Location lastLocation=tracker.getLocation();





        PersistObject.apply(con,AppConstant.LOCATION,lastLocation);
        Log.e("Location is refreshed", "5 seconds remaining");


    }
}

对于为什么会发生这种情况,我感到非常困惑,我已经花了数小时,并雇用了一些自由职业者来提供帮助,而他们所做的任何事情都无法解决这个问题。该代码将有一半时间正常工作,它将发送设备的新位置,一半时间将发送设备先前返回的位置。我四处移动电话以测试新的地点,然后在三部不同的电话上运行该电话,并且断断续续地发生在所有电话上。任何想法将不胜感激。

0 个答案:

没有答案