In many tutorials, the updating of a widget is suggested to be as follows;
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
int widgetId = appWidgetIds[i];
Log.d(TAG, " ## onUpdate ## , appWidgetIds.length = "+ String.valueOf(appWidgetIds.length) + " appWidgetId = " + widgetId );
context.startService(new Intent(context, UpdateService.class));
}
}
However, though there is only one widget on my homescreen, the size of appWidgetIds returns 52 and each one of them tries to process an update. This, of course, causes an overload on the called service, eventually crashing the application.
In his post, Kostya Vasilyev offers a workaround for this bug.
This should be quite a common issue but I couldnT find any alternatives to this solution. How could we identify the stale widgets and avoid trying to update them?
Thanks in advance.
EDIT : I ve just figured one good way to avoid stale widgets is to stop the running service (if there is any), upon the completion of the code in it :) This seems to have sa开发者_如何学Cved my day, of course it s not the answer though.
精彩评论