Thanks for the tip; it resulted in much more code, but I think the
demands on the system should be smaller now.  For others who might
read this, my implementation looks like this (Mark, feel free to
correct mistakes):

private Handler mGPSTimeOut;
private Runnable mGPSCallback = null;
private long mLastFix = 0;

protected void onCreate() {
    mGPSTimeOut = new Handler();
}

protected void onResume() {
    mGPSCallback = getGPSTimeOutHandler();
    mGPSTimeOut.postDelayed(mGPSCallback, 500);
}

protected void onPause() {
    if (mGPSCallback != null) mGPSTimeOut.removeCallbacks
(mGPSCallback); //Not sure if this is necessary
}

private Runnable getGPSTimeOutHandler() {
    return new Runnable() {
        public void run() {
            if (System.currentTimeMillis() - mLastFix > 4500) {
                mglvArrow.setColor(Arrow.ColorOptions.RED);
                mGPSCallback = null;
            } else if (System.currentTimeMillis() - mLastFix > 1300) {
                mglvArrow.setColor(Arrow.ColorOptions.YELLOW);
                mGPSCallback = getGPSTimeOutHandler();
                mGPSTimeOut.postDelayed(mGPSCallback, 500);
            }
        }
    };
}

public void onLocationChanged(Location location) {
    mLastFix = location.getTime();
    if (mGPSCallback == null) {
        mGPSCallback = getGPSTimeOutHandler();
        mGPSTimeOut.postDelayed(mGPSCallback, 500);
    }
}

--Ben

On Sep 17, 3:08 am, Mark Murphy <[email protected]> wrote:
> BJP wrote:
> > I'm writing an application where real-time knowledge of the GPS state
> > is critical to convey to the user.  I request GPS updates at 1000 ms
> > intervals -- when I haven't received another update 1500 ms past the
> > most recent update, I want to display a yellow icon, and 5000 ms after
> > the most recent update I want to display a red icon.  Currently, I'm
> > doing it like this:
>
> > private CountDownTimer gpstimeout;
>
> > public void onLocationChanged(Location location) {
> >     if (gpstimeout != null) gpstimeout.cancel();
> >     gpstimeout = new CountDownTimer(5000, 1500) {
> >         public void onTick(long m) { setYellow(); }
> >         public void onFinish() { setRed(); }
> >     };
> >     gpstimeout.start();
> > }
>
> > This works well (except that onTick appears to be called immediately
> > on creation of the new CountDownTimer), but it seems like creating and
> > destroying a new thread every second wouldn't be the best way to do
> > things.  Is there a better approach to this?
>
> Here is how I would approach it, FWIW:
>
> Step #1: Have a private long data member named lastFix in your activity.
>
> Step #2: Update lastFix to be the current time in onLocationChanged().
>
> Step #3: Use postDelayed() on View to set up a periodic quasi-loop
> (i.e., the Runnable to postDelayed() calls postDelayed() to reschedule
> the same Runnable after your desired period)
>
> Step #4: The Runnable from Step #3 can then look at lastFix and update
> your UI as needed if your fix gets stale
>
> I would not go *too* tight on this quasi-loop. I have used a one-second
> loop this way with no ill effects, but I would be nervous about it
> being, say, every 100ms.
>
> --
> Mark Murphy (a Commons 
> Guy)http://commonsware.com|http://twitter.com/commonsguy
>
> _Android Programming Tutorials_ Version 1.0 In Print!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to