Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ids of all the widgets installed?

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.

like image 958
artem Avatar asked Feb 19 '23 04:02

artem


1 Answers

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).

like image 66
Juan Cortés Avatar answered Mar 08 '23 03:03

Juan Cortés