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