WidgetProvider receives broadcast while there is no widget displayed
There is something I really don't understand with the Android Widget API.
I wrote a widget with a configuration Activity, and each time I install the application with adb, WidgetProvider.onUpdate()
is called even if the widget isn't displayed. And also sometimes with multiple appWidgetIds, and also when the configuration Activity is started.
In the onUpdate
method I checked the value of appWidgetManager.getAppWidgetIds()
, and it returns 0 items.
public class FlickrWidget
extends AppWidgetProvider
{
/**
* @see android.appwidget.AppWidgetProvider#onUpdate(android.content.Context, android.appwidget.AppWidgetManager, int[])
*/
@Override
public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds )
{
Log.d( "Flic开发者_运维百科krWidget: Start onUpdate with " + appWidgetIds.length + " widgets" );
int[] appWidgetIds2 = appWidgetManager.getAppWidgetIds( new ComponentName( context, FlickrWidget.class ) );
Log.d( "FlickrWidget: installed : " + appWidgetIds2.length + " widgets" ); //display 0 items
for ( int i = 0; i < appWidgetIds2.length; i++ )
{
Log.d( "FlickrWidget : installed appWidgetId = " + appWidgetIds2[i] );
}
for ( int i = 0; i < appWidgetIds.length; i++ )
{
Log.d( "FlickrWidget : appWidgetId = " + appWidgetIds[i] ); // can have multiple items
}
}
}
I don't understand because I though :
- An
AppWidgetProvider
receives the broadcast only when there is one ore more widget displayed. - A widget with a configuration
Activity
isn't updated with the broadcast on start up. - Calling
appWidgetManager.getAppWidgetIds()
inonUpdate
should returns the same int array than theappWidgetIds
in the parameters.
As I experiment all those 3 dots are false...
Can someone help me ?
Thanks in advance
Finally I repeated this question on Android Google Groups and a nice guy answers me that the Widget api documentation on developer.android.com is obsolete...
https://groups.google.com/d/topic/android-developers/HfD-ojjsuso/discussion
精彩评论