8

I'm trying to read the available home screen widgets list on Android. I can populate a grid using the available applications list using

Intent myIntent = new Intent(Intent.ACTION_MAIN, null);
myIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> appsInfo = MyActivity.getPackageManager().queryIntentActivities(myIntent, 0);

and than iterating through each ResolveInfo.
How can I do the same with available Home screen widgets? I'd like to populate a grid with the same list that appears keep touching the screen and choosing 'widget' from the appearing popup.

Kara
  • 6,115
  • 16
  • 50
  • 57
lorenzoff
  • 1,120
  • 3
  • 15
  • 32

2 Answers2

14

As suggested by CommonsWare, here is the working code for extracting list of widgets

AppWidgetManager manager = AppWidgetManager.getInstance(this);
List<AppWidgetProviderInfo> infoList = manager.getInstalledProviders();
for (AppWidgetProviderInfo info : infoList) {
    Log.d(TAG, "Name: " + info.label);
    Log.d(TAG, "Provider Name: " + info.provider);
    Log.d(TAG, "Configure Name: " + info.configure);
}

Various other values can be extracted, for more reference see AppWidgetProviderInfo

TWiStErRob
  • 44,762
  • 26
  • 170
  • 254
Akhil Jain
  • 13,872
  • 15
  • 57
  • 93
  • Is is possible to get the icon of the associated widget with this? – Ushal Naidoo May 13 '14 at 03:21
  • @RandyFreak yes we can extract icon from this – Akhil Jain Sep 18 '14 at 08:28
  • how do i get launching widgets which are from my app please? – Beeing Jk Jul 27 '18 at 10:39
  • @BeeingJk What do you mean exactly, launching widgets from your application or find and run widgets which you have created..? – Akhil Jain Jul 27 '18 at 11:47
  • @AkhilJain I actually want to know when the last widget from my app is deleted/removed from device desktop, so i need to get only the widgets from my app since getInstalledProviders() returns all widgets including those owned by other app, as meaning to find out the already created/running/installed widgets which belongs to my app. – Beeing Jk Jul 30 '18 at 02:08
  • 1
    @BeeingJk this might help you with your query - https://stackoverflow.com/a/8837570/1225413 – Akhil Jain Jul 30 '18 at 06:28
  • at this line val manager = AppWidgetManager.getInstance(context), I am getting "java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List android.appwidget.AppWidgetManager.getInstalledProviders()' on a null object reference" – shripal Jul 07 '23 at 12:35
10

Call getInstalledProviders() on an AppWidgetManager.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • This will sometime throw android.os.TransactionTooLargeException. I can't work out why... – Bob bobbington Aug 10 '17 at 10:30
  • 2
    @Bobbobbington: That would happen if there are lots of `AppWidgetProviders` on the device. There is a 1MB limit on all simultaneous IPC operations from your process, so if the result of `getInstalledProviders()` is over 1MB, you will get this exception. Even if the `getInstalledProviders()` list is smaller, if you happen to call it while some other IPC in your process is going on (and consuming some of the 1MB limit), you will get this exception. – CommonsWare Aug 10 '17 at 11:09