Picture of the Week 09 Mar 2011 01:16 pm

Spring Flower

Spring Flower

Well, it might be snowing outside right now, but at least the house plants think it’s spring.

Android 29 Dec 2010 02:29 pm

Periodically Updating an Android UI

I just spent a while sorting out how to cause an Android user interface to update periodically, so I thought I would post the details. You need periodic user interface updates whenever you have UI elements that you want to update based on time, rather than user or system events. A clock would be a perfect example. Also, you can use this when a data source, such as a sensor, like an accelerometer or the GPS, is giving you updates faster than the speed you want the UI to update at. This implementation uses a message handler and the sendEmptyMessageDelayed() method to implement a callback timer, without using timers or threads. The downside is that all of the timings are approximate and variable, so don’t use this where precise timing is required.

Starting at the top, this class uses the Calendar to get the current time, and extends the android Handler class. This example assumes your user interface implements an updateDisplay() method, which is called in the checkTime() method of this class:

import java.util.Calendar;
 
import android.os.Handler;
import android.os.Message;
 
// This class implements a message loop based approximate timer to
// control the periodic update of the user interface.
// This class check if the UI needs to be updated at the SPIN_PERIOD in
// milliseconds, and if more that the REFRESH_PERIOD has passed, the
// user interface is updated.  With the refresh period at 1000ms and
// the spin period at 100ms, this class will cause the UI to update
// every 1.0+/-0.1 seconds.
 
public class UpdateTimer extends Handler
{
    public final static int MSG_START = 0;
    public final static int MSG_STOP = 1;
    public final static int MSG_UPDATE = 2;
    public final static int REFRESH_PERIOD = 1000; // in ms
    public final static int SPIN_PERIOD = 100; // in ms 
 
    // pointer to the user interface adapter
    private DebugUIAdapter mUI;
    // remember the last time the UI was updated
    private long mLastTime;
 
    public UpdateTimer(DebugUIAdapter theUI)
    {
        super();
        mUI = theUI;
        mLastTime = 0;
    }
 
    // handle messages to implement the screen refresh timer
    @Override
    public void handleMessage(Message msg)
    {
        super.handleMessage(msg);
 
        switch (msg.what)
        {
        case MSG_START:
            // start the timer by sticking an update message
            // into the message queue
            this.sendEmptyMessage(MSG_UPDATE);
            break;
 
        case MSG_UPDATE:
            // the timer loops by sticking delayed messages into the
            // queue at the spin period.  Each spin period, use
            // checkTime() to see if a UI update is required
            this.checkTime();
            this.sendEmptyMessageDelayed(MSG_UPDATE, SPIN_PERIOD);
            break;                                 
 
        case MSG_STOP:
            // stop the timer by removing any and all delayed update
            // messages that have not been processed
            this.removeMessages(MSG_UPDATE);
            break;
 
        default:
            break;
        }
    }
 
    // check how much time has passed and update the UI
    // if required.
    private void checkTime()
    {
        // how much time has passed since the last update?
        long currTime = Calendar.getInstance().getTimeInMillis();
        long dt = currTime - mLastTime;
        // if it is more than the refresh period, update the UI
        if (dt > REFRESH_PERIOD)
	{
	    mUI.updateDisplay();
	    mLastTime = currTime;
	}
    }
}

To use this timer, the code in your activity might look something like the following. By stopping and starting the timer in the pause and resume callbacks, the UI updates will only be called when your UI is in the foreground:

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        mUI = new DebugUIAdapter();
        mTimer = new UpdateTimer(mUI);
    }
 
    @Override
    protected void onPause()
    {
        super.onPause();
	mTimer.sendEmptyMessage(UpdateTimer.MSG_STOP);
    }
 
    @Override
    protected void onResume()
    {
        super.onResume();
        mTimer.sendEmptyMessage(UpdateTimer.MSG_START);
    }

Resources I came across that helped sort this out include:

http://developer.android.com/resources/articles/timed-ui-updates.html

http://efreedom.com/Question/1-3733867/Stop-Watch-Logic#3734070

Lifestyle 21 Oct 2010 04:16 pm

Northern Flicker

Northern Flicker

…having lunch in the backyard…

Lifestyle 02 Aug 2010 09:55 am

Long Point Provincial Park

Eva Paddling

We have been down to Long Point Provincial Park a couple of times now. On lake Erie, the park is a 20km long sand spit. On side is sand beach with decent surf. The back side is endless marshland with lots of birds of all descriptions.

Next Page »