开发者

WidgetProvider receives broadcast while there is no widget displayed

开发者 https://www.devze.com 2023-02-18 06:15 出处:网络
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()

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 :

  1. An AppWidgetProvider receives the broadcast only when there is one ore more widget displayed.
  2. A widget with a configuration Activity isn't updated with the broadcast on start up.
  3. Calling appWidgetManager.getAppWidgetIds() in onUpdate should returns the same int array than the appWidgetIds 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

0

精彩评论

暂无评论...
验证码 换一张
取 消