Minor question about Elapsed Time on Emulator


#1

I built the program from Chapter 33 (Tracking the Device’s Location) and all works well on the phone. And almost everything works well on the emulator. I use DDMS to manually change the latitude and longitude and they are correctly reported on the screen. What appears wrong is the elapsed time. It starts out (for example) at -24:-1:-25 and progresses to -24:-1:-23. I debugged through the code, and the durationTime is negative. It’s negative because the time of the gps measurement is less than the start time. I don’t think DDMS is settng an up to date time. Has anyone else noticed this?


#2

UPDATE: Even better, just ignore all the sync stuff I said and pass in new Date().getTime() to the getDurationSeconds function like so: durationSeconds = mRun.getDurationSeconds(new Date().getTime());

OLD ANSWER:
Yes, I am also seeing this on the emulator. From reading online, the gps time is independent of system time, so I created the following function to sync them up, and now it works fine in the emulator:

private void syncGpsAndSystemTime() { long timeDiff = new Date().getTime() - mlastLocation.getTime(); mlastLocation.setTime(mlastLocation.getTime() + timeDiff); }

and I call the sync every time a location is received:

[code]@Override
protected void onLocationReceived(Context context, Location loc) {
mlastLocation = loc;

syncGpsAndSystemTime();

if (isVisible()) {
    updateUI();
}

}[/code]


#3

in Run.java

    public int getDurationSeconds(long endMillis) {
        return (int) ((endMillis - mStartDate.getTime()) / 1000);
    }

in RunFragment.java

[code]private void updateUI() {
    ......

    if (mRun != null && mLastLocation != null) {
        durationSeconds = mRun.getDurationSeconds(mLastLocation.getTime());
        mLatitudeTextView.setText(Double.toString(mLastLocation.getLatitude()));
        mLongitudeTextView.setText(Double.toString(mLastLocation.getLongitude()));
        mAltitudeTextView.setText(Double.toString(mLastLocation.getAltitude()));
    }

    mDurationTextView.setText(Run.formatDuration(durationSeconds));
}[/code]

you use the mLastLocation to calculate the durationSeconds.

the first time you click the “Start”, everything is OK. then you click “Stop”.
ths second time you click the “Start”, because the mLastLocation is not null and mLastLocation.getTime() is before mStartDate.getTime(), so you got a nagtive durationTime.

in my practice, i made the mLastLocation be null every time you click the “Start” button.

        mStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRunManager.startLocationUpdates();
                mRun = new Run();
                mLastLocation = null;
                updateUI();
            }
        });