5

I need to find any AppWidget id to test some features. The code to do this:

List<AppWidgetProviderInfo> infos = mAppWidgetManager.getInstalledProviders();

        for (int i = 0; i < infos.size(); i++) {
            int ids[] = mAppWidgetManager.getAppWidgetIds(infos.get(i).provider);
            if (ids.length != 0) {
                Log.d("TAG", "Found non-zero-length provider!");
                id = ids[0];
                info = infos.get(i);
                break;
            }
        }

But ids[] length is always 0! Why and how to fix it?

Update: I have added this line to the loop's beginning:

Log.d("TAG", provider.getPackageName() + "; " + provider.getClassName());

And it prints:

D/TAG     ( 5686): Providers count: 12
D/TAG     ( 5686): com.andrew.apollo; com.andrew.apollo.app.widgets.AppWidget11
D/TAG     ( 5686): com.andrew.apollo; com.andrew.apollo.app.widgets.AppWidget41
D/TAG     ( 5686): com.andrew.apollo; com.andrew.apollo.app.widgets.AppWidget42
D/TAG     ( 5686): com.android.browser; com.android.browser.widget.BookmarkThumbnailWidgetProvider
D/TAG     ( 5686): com.android.calendar; com.android.calendar.widget.CalendarAppWidgetProvider
D/TAG     ( 5686): com.android.contacts; com.android.contacts.socialwidget.SocialWidgetProvider
D/TAG     ( 5686): com.android.deskclock; com.android.alarmclock.AnalogAppWidgetProvider
D/TAG     ( 5686): com.android.email; com.android.email.provider.WidgetProvider
D/TAG     ( 5686): com.android.gallery3d; com.android.gallery3d.gadget.PhotoAppWidgetProvider
D/TAG     ( 5686): com.android.quicksearchbox; com.android.quicksearchbox.SearchWidgetProvider
D/TAG     ( 5686): com.android.settings; com.android.settings.widget.SettingsAppWidgetProvider
D/TAG     ( 5686): com.android.vending; com.android.vending.MarketWidgetProvider

So, it seems to be OK, but I still can't get id's of this widgets.

Tim
  • 35,413
  • 11
  • 95
  • 121
artem
  • 16,382
  • 34
  • 113
  • 189

3 Answers3

5

Make sure that ComponentName you use to get AppWidgetManager matches the widget provider in the manifest. If it doesn't, you'll get an empty list (ids.length = 0, the loop won't execute).

Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
  • I'm unsure if you can retrieve the ids of the Widgets that don't belong to your application, someone may clarify this point for us – Juan Cortés Sep 12 '12 at 06:18
3

In Koltin like so:

val widgetManager = AppWidgetManager.getInstance(context).getAppWidgetIds(ComponentName(context, MyAppWidget::class.java))
Thiago
  • 12,778
  • 14
  • 93
  • 110
2

I you have different layouts and sizes for your widgets and thus different widget providers retrieving all id's turned out to be not that simple. To get all widget id's for all your widget providers you can do:

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    int Widged_IDS_A[] = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetProviderA.class));
    int Widged_IDS_B[] = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetProviderB.class));
    int Widged_IDS_C[] = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetProviderC.class));
    int Widged_IDS_D[] = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetProviderD.class));
    int Widged_IDS_E[] = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetProviderE.class));

    int allWidgetIds[] = new int[Widged_IDS_A.length + Widged_IDS_B.length + Widged_IDS_C.length + Widged_IDS_D.length + Widged_IDS_E.length];

    System.arraycopy(Widged_IDS_A, 0, allWidgetIds, 0, Widged_IDS_A.length);
    System.arraycopy(Widged_IDS_B, 0, allWidgetIds, Widged_IDS_A.length, Widged_IDS_B.length);
    System.arraycopy(Widged_IDS_C, 0, allWidgetIds, Widged_IDS_A.length + Widged_IDS_B.length, Widged_IDS_C.length);
    System.arraycopy(Widged_IDS_D, 0, allWidgetIds, Widged_IDS_A.length + Widged_IDS_B.length + Widged_IDS_C.length, Widged_IDS_D.length);
    System.arraycopy(Widged_IDS_E, 0, allWidgetIds, Widged_IDS_A.length + Widged_IDS_B.length + Widged_IDS_C.length + Widged_IDS_D.length, Widged_IDS_E.length);
    Log.v(Classname, "allWidgetIds.length: " + allWidgetIds.length);

    allWidgetIds = merge(Widged_IDS_A, Widged_IDS_B, Widged_IDS_C, Widged_IDS_D, Widged_IDS_E);
    for (int x : allWidgetIds) {
         Log.v(Classname, x + " allWidgetIds onReceive()");
    }
Mark
  • 156
  • 1
  • 7