views:

12

answers:

0

Hi,

I'm trying to develop something like Digital Clock widget and I'm using http://nm-blog.sanid.com/wp-content/uploads/2009/07/android_howto-hellowidget.pdf as tutorial (TimerTask part). Problem is, that after starting other app or locking screen widget stops updating.

Currently I'm using this code and I have a feeling that something is missing in onDisabled/onEnabled/onUpdate events or maybe old Android 1.6 SDK doing something wrong (developing for my phone with 1.6)?

public class HelloWidget extends AppWidgetProvider {
    Timer timer;

    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
        timer = null;
    }

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        timer = null;
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        if (timer == null) {
            timer = new Timer();
            timer.scheduleAtFixedRate(new ClockUpdate(context, appWidgetManager), 1, 1000);
        } else {
            timer = null;
        }

    public class ClockUpdate extends TimerTask {...}
    }

And another question, what is lifecycle of widget? There are plenty descriptions of application widget, but couldn't find one for widget.