Friday, July 15, 2011

[android-developers] Weird java.util.Timer behavior in App Widget

Hey guys,

I have an app widget which looks like this.

public class MyWidget extends AppWidgetProvider
{
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
public Timer timer = new Timer();
public int count = 0;

@Override
public void onUpdate(Context context, AppWidgetManager
appWidgetManager, int[] appWidgetIds)
{
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.main);

Intent active = new Intent(context, MyWidget.class);
active.setAction(ACTION_WIDGET_RECEIVER);
active.putExtra("msg", "Message for Button 1");

PendingIntent actionPendingIntent =
PendingIntent.getBroadcast(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.button_one,
actionPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}

@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER))
{
final AppWidgetManager appWidgetManager =
AppWidgetManager.getInstance(context);
final ComponentName thisWidget = new ComponentName(context,
MyWidget.class.getName());

count = 0;
timer.cancel();
timer.purge();

timer = new Timer();
timer.scheduleAtFixedRate(new UpdatedTimerTask(this, context,
appWidgetManager), 0, 1000);
}

super.onReceive(context, intent);
}
}

My UpdatedTimerTask looks like this.

public class UpdatedTimerTask extends TimerTask
{
private Context context;
private AppWidgetManager appWidgetManager;

private RemoteViews remoteViews;
private ComponentName thisWidget;

private MyWidget widget;

public UpdatedTimerTask(MyWidget widget, Context context,
AppWidgetManager appWidgetManager)
{
this.widget = widget;
this.context = context;
this.appWidgetManager = appWidgetManager;

this.remoteViews = new RemoteViews(context.getPackageName(),
R.layout.main);
this.thisWidget = new ComponentName(context, MyWidget.class);
}

@Override
public void run()
{
this.remoteViews.setTextViewText(R.id.label3, "Count: " +
widget.count);
this.appWidgetManager.updateAppWidget(this.thisWidget,
this.remoteViews);
widget.count ++;
}
}

When I press Button1 on this widget its acts as I would expect. The
count gets updated every second and displays in label3.
Button1
Count: 1
Count: 2
Count: 3
etc

However when I press Button1 again I expect the count to reset to 0
and continue from there.
Whats actually happening is the two Timers are firing overlapped.

Button1
Count: 1
Count: 2
Count: 3
Button1
Count: 1
Count: 4
Count: 2
etc

Why aren't my calls to timer.cancel() and timer.purge() stopping the
first timer before replacing it?
How do I fix this issue???

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment