Hi,
I have a widget that displays usage data from a remote service. To
avoid tying up the UI thread I use a worker thread to do the HTTP
fetch and parsing, which then send the results back to the widget
provider via a message/handler. However at the point that the handler
is called with the result the widget-provider no-longer has access to
the original parameters with which it was called. Is it valid to save
the context, etc. in the provider for later use? My concern is that
if another update fires while the worker is away the parameters would
have changed.
An (simplified) example to show the problem:
public class WidgetUpdater
extends AppWidgetProvider
implements Handler.Callback
{
private ExecutorService pool = Executors.newSingleThreadExecutor();
// Save variables
private Context context;
private AppWidgetManager appWidgetManager;
private int[] appWidgetIds;
@Override
public void onUpdate(Context context, AppWidgetManager
appWidgetManager, int[] appWidgetIds)
{
String uri = "http://usage.example.com/";
// Q: Can I save the context, manager and widget-ids for later use?
// this.context = context;
// this.appWidgetManager = appWidgetManager;
// this.appWidgetIds = appWidgetIds;
// Fetch usage in a sub-thread
HttpUsageFetcher usageWorker = new HttpUsageWorker(new
Handler(this), uri);
pool.execute(usageWorker);
}
@Override
public boolean handleMessage(Message msg) {
// Q: Update may have been called again at this point. Does
this matter?
if (msg.what == GOTRESULT) {
Usage usage = (Usage)msg.obj;
// Q: At this point I need at least the context,
preferably all the parameters to onUpdate()
// for (int id: appWidgetIds) {
// RemoteViews views = new
RemoteViews(context.getPackageName(), R.layout.widget);
// views.setProgressBar(R.id.widget_progress, 100,
usage.getPercentageUsed(), false);
// appWidgetManager.updateAppWidget(appWidgetId, views);
// }
}
return true;
}
}
Cheers,
Steve
--
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