# GlanceAppWidgetManager

Artifact: [androidx.glance:glance-appwidget](https://developer.android.com/jetpack/androidx/releases/glance) [View Source](https://cs.android.com/search?q=file:androidx/glance/appwidget/GlanceAppWidgetManager.kt+class:androidx.glance.appwidget.GlanceAppWidgetManager) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/glance#1.0.0)


```
class GlanceAppWidgetManager
```

<br />

*** ** * ** ***

Manager for Glance App Widgets.

This is used to query the app widgets currently installed on the system, and some of their properties.

## Summary

| ### Constants |
|---|---|
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#SET_WIDGET_PREVIEWS_RESULT_RATE_LIMITED() = 1` Return from `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#setWidgetPreviews(kotlin.reflect.KClass,androidx.collection.IntSet)` to indicate that the operation was not successful due to a rate limit. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#SET_WIDGET_PREVIEWS_RESULT_SUCCESS() = 0` Returned from `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#setWidgetPreviews(kotlin.reflect.KClass,androidx.collection.IntSet)` to indicate that the previews were set successfully. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#GlanceAppWidgetManager(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html)` |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#getAppWidgetId(androidx.glance.GlanceId)(glanceId: https://developer.android.com/reference/kotlin/androidx/glance/GlanceId)` Retrieve the platform AppWidget ID from the provided GlanceId |
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/DpSize>` | `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#getAppWidgetSizes(androidx.glance.GlanceId)(glanceId: https://developer.android.com/reference/kotlin/androidx/glance/GlanceId)` Retrieve the sizes for a given App Widget, if provided by the host. |
| `https://developer.android.com/reference/kotlin/androidx/glance/GlanceId` | `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#getGlanceIdBy(kotlin.Int)(appWidgetId: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Retrieve the GlanceId of the provided AppWidget ID. |
| `https://developer.android.com/reference/kotlin/androidx/glance/GlanceId?` | `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#getGlanceIdBy(android.content.Intent)(configurationIntent: https://developer.android.com/reference/android/content/Intent.html)` Retrieve the GlanceId from the configuration activity intent or null if not valid |
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/glance/GlanceId>` | `<T : https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget> https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#getGlanceIds(java.lang.Class)(provider: https://developer.android.com/reference/java/lang/Class.html<T>)` Returns the `https://developer.android.com/reference/kotlin/androidx/glance/GlanceId` of the App Widgets installed for a particular provider. |
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `<T : https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetReceiver> https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#requestPinGlanceAppWidget(java.lang.Class,androidx.glance.appwidget.GlanceAppWidget,kotlin.Any,android.app.PendingIntent)( receiver: https://developer.android.com/reference/java/lang/Class.html<T>, preview: https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget?, previewState: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, successCallback: https://developer.android.com/reference/android/app/PendingIntent.html? )` Request to pin the `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget` of the given receiver on the current launcher (if supported). |
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `<T : https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetReceiver> https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#requestPinGlanceAppWidget(java.lang.Class,androidx.glance.appwidget.GlanceAppWidget,androidx.compose.ui.unit.DpSize,kotlin.Any,android.app.PendingIntent)( receiver: https://developer.android.com/reference/java/lang/Class.html<T>, preview: https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget?, previewSize: https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/DpSize?, previewState: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, successCallback: https://developer.android.com/reference/android/app/PendingIntent.html? )` Request to pin the `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget` of the given receiver on the current launcher (if supported). |
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/RequiresApi(value = 35) https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#setWidgetPreviews(kotlin.reflect.KClass,androidx.collection.IntSet)( receiver: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetReceiver>, widgetCategories: https://developer.android.com/reference/kotlin/androidx/collection/IntSet )` Generate and publish the widget previews for `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#setWidgetPreviews(kotlin.reflect.KClass,androidx.collection.IntSet)` for the given set of `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#setWidgetPreviews(kotlin.reflect.KClass,androidx.collection.IntSet)`. |

| ### Extension functions |
|---|---|
| `suspend inline https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/RequiresApi(value = 35) <T : https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetReceiver> https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager.https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#(androidx.glance.appwidget.GlanceAppWidgetManager).setWidgetPreviews(androidx.collection.IntSet)( widgetCategories: https://developer.android.com/reference/kotlin/androidx/collection/IntSet )` Generate and publish the widget previews for a `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetReceiver` for the given set of `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/package-summary#(androidx.glance.appwidget.GlanceAppWidgetManager).setWidgetPreviews(androidx.collection.IntSet)`. |

## Constants

### SET_WIDGET_PREVIEWS_RESULT_RATE_LIMITED

```
const val SET_WIDGET_PREVIEWS_RESULT_RATE_LIMITED = 1: Int
```

Return from `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#setWidgetPreviews(kotlin.reflect.KClass,androidx.collection.IntSet)` to indicate that the operation was not successful due to a rate limit.

### SET_WIDGET_PREVIEWS_RESULT_SUCCESS

```
const val SET_WIDGET_PREVIEWS_RESULT_SUCCESS = 0: Int
```

Returned from `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#setWidgetPreviews(kotlin.reflect.KClass,androidx.collection.IntSet)` to indicate that the previews were set successfully.

## Public constructors

### GlanceAppWidgetManager

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/glance#1.0.0)

```
GlanceAppWidgetManager(context: Context)
```

## Public functions

### getAppWidgetId

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/glance#1.0.0)

```
fun getAppWidgetId(glanceId: GlanceId): Int
```

Retrieve the platform AppWidget ID from the provided GlanceId

Important: Do NOT use appwidget ID as identifier, instead create your own and store them in the GlanceStateDefinition. This method should only be used for compatibility or IPC communication reasons in conjunction with `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#getGlanceIdBy(kotlin.Int)`

### getAppWidgetSizes

```
suspend fun getAppWidgetSizes(glanceId: GlanceId): List<DpSize>
```

Retrieve the sizes for a given App Widget, if provided by the host.

The list of sizes will be extracted from the App Widget options bundle, using the content of `https://developer.android.com/reference/android/appwidget/AppWidgetManager.html#OPTION_APPWIDGET_SIZES` if provided. If not, and if `https://developer.android.com/reference/android/appwidget/AppWidgetManager.html#OPTION_APPWIDGET_MIN_HEIGHT` and similar are provided, the landscape and portrait sizes will be estimated from those and returned. Otherwise, the list will contain `https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/DpSize#Zero()` only.

### getGlanceIdBy

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/glance#1.0.0)

```
fun getGlanceIdBy(appWidgetId: Int): GlanceId
```

Retrieve the GlanceId of the provided AppWidget ID.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if the provided id is not associated with an existing GlanceId |

### getGlanceIdBy

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/glance#1.0.0)

```
fun getGlanceIdBy(configurationIntent: Intent): GlanceId?
```

Retrieve the GlanceId from the configuration activity intent or null if not valid

### getGlanceIds

```
suspend fun <T : GlanceAppWidget> getGlanceIds(provider: Class<T>): List<GlanceId>
```

Returns the `https://developer.android.com/reference/kotlin/androidx/glance/GlanceId` of the App Widgets installed for a particular provider.

### requestPinGlanceAppWidget

```
suspend fun <T : GlanceAppWidgetReceiver> requestPinGlanceAppWidget(
    receiver: Class<T>,
    preview: GlanceAppWidget? = null,
    previewState: Any? = null,
    successCallback: PendingIntent? = null
): Boolean
```

Request to pin the `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget` of the given receiver on the current launcher (if supported).

Note: the request is only supported for SDK 26 and beyond, for lower versions this method will be no-op and return false.

| Parameters |
|---|---|
| `receiver: https://developer.android.com/reference/java/lang/Class.html<T>` | the target `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetReceiver` class |
| `preview: https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget? = null` | the instance of the GlanceAppWidget to compose the preview that will be shown in the request dialog. When not provided the app widget previewImage (as defined in the meta-data) will be used instead, or the app's icon if not available either. |
| `previewState: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html? = null` | the state (as defined by the `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget#stateDefinition()` to use for the preview |
| `successCallback: https://developer.android.com/reference/android/app/PendingIntent.html? = null` | a `https://developer.android.com/reference/android/app/PendingIntent.html` to be invoked if the app widget pinning is accepted by the user |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | true if the request was successfully sent to the system, false otherwise |

| See also |
|---|---|
| `https://developer.android.com/reference/android/appwidget/AppWidgetManager.html#requestPinAppWidget(android.content.ComponentName, android.os.Bundle, android.app.PendingIntent)` | for more information and limitations |

### requestPinGlanceAppWidget

```
suspend fun <T : GlanceAppWidgetReceiver> requestPinGlanceAppWidget(
    receiver: Class<T>,
    preview: GlanceAppWidget? = null,
    previewSize: DpSize? = null,
    previewState: Any? = null,
    successCallback: PendingIntent? = null
): Boolean
```

Request to pin the `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget` of the given receiver on the current launcher (if supported).

Note: the request is only supported for SDK 26 and beyond, for lower versions this method will be no-op and return false.

| Parameters |
|---|---|
| `receiver: https://developer.android.com/reference/java/lang/Class.html<T>` | the target `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetReceiver` class |
| `preview: https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget? = null` | the instance of the GlanceAppWidget to compose the preview that will be shown in the request dialog. When not provided the app widget previewImage (as defined in the meta-data) will be used instead, or the app's icon if not available either. |
| `previewSize: https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/DpSize? = null` | the size to be used for the preview. If none is provided, the widget's minimum size (as determined by its' AppWidgetProviderInfo) will be used. |
| `previewState: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html? = null` | the state (as defined by the `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget#stateDefinition()` to use for the preview |
| `successCallback: https://developer.android.com/reference/android/app/PendingIntent.html? = null` | a `https://developer.android.com/reference/android/app/PendingIntent.html` to be invoked if the app widget pinning is accepted by the user |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | true if the request was successfully sent to the system, false otherwise |

| See also |
|---|---|
| `https://developer.android.com/reference/android/appwidget/AppWidgetManager.html#requestPinAppWidget(android.content.ComponentName, android.os.Bundle, android.app.PendingIntent)` | for more information and limitations |

### setWidgetPreviews

```
@RequiresApi(value = 35)
suspend fun setWidgetPreviews(
    receiver: KClass<GlanceAppWidgetReceiver>,
    widgetCategories: IntSet = intSetOf(
                WIDGET_CATEGORY_HOME_SCREEN or WIDGET_CATEGORY_KEYGUARD or WIDGET_CATEGORY_SEARCHBOX
            )
): Int
```

Generate and publish the widget previews for `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#setWidgetPreviews(kotlin.reflect.KClass,androidx.collection.IntSet)` for the given set of `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#setWidgetPreviews(kotlin.reflect.KClass,androidx.collection.IntSet)`. Previews are generated from the layout provided by `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget#providePreview(android.content.Context,kotlin.Int)` on the widget connected to the given `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#setWidgetPreviews(kotlin.reflect.KClass,androidx.collection.IntSet)`.

Previews should be published during the initial setup phase or launch of your app. To avoid running this unnecessarily, you can see what previews are currently published for your provider by checking `https://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html#generatedPreviewCategories`.

The preview composition is run for each value in the `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#setWidgetPreviews(kotlin.reflect.KClass,androidx.collection.IntSet)` array. By default, a single preview is generated for all widget categories, i.e. `widgetsCategories = intSetOf(WIDGET_CATEGORY_HOME_SCREEN or WIDGET_CATEGORY_KEYGUARD or WIDGET_CATEGORY_SEARCHBOX)`. To generate a separate preview for each widget category, pass each category as a separate item in the int set, e.g. `intSetOf(WIDGET_CATEGORY_HOME_SCREEN, WIDGET_CATEGORY_KEYGUARD)`. This is only necessary if you want to generate different layouts for the different categories.

Note that this API is only available on `https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#VANILLA_ICE_CREAM` and above, so you will likely want to set `https://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html#previewLayout` and `https://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html#previewImage` as well to have the most coverage across versions.

See also `https://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html#generatedPreviewCategories`, `https://developer.android.com/reference/android/appwidget/AppWidgetManager.html#setWidgetPreview(android.content.ComponentName, int, android.widget.RemoteViews)`, `https://developer.android.com/reference/android/appwidget/AppWidgetManager.html#getWidgetPreview(android.content.ComponentName, android.os.UserHandle, int)`, and `https://developer.android.com/reference/android/appwidget/AppWidgetManager.html#removeWidgetPreview(android.content.ComponentName, int)`.

```kotlin
import androidx.glance.appwidget.GlanceAppWidgetManager
import androidx.glance.appwidget.GlanceAppWidgetManager.Companion.SET_WIDGET_PREVIEWS_RESULT_RATE_LIMITED
import androidx.glance.appwidget.GlanceAppWidgetReceiver

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM) {
    return
}
fun Class<GlanceAppWidgetReceiver>.hasPreviewForCategory(widgetCategory: Int): Boolean {
    val component = ComponentName(context, this)
    val providerInfo =
        (context.getSystemService(Context.APPWIDGET_SERVICE) as AppWidgetManager)
            .installedProviders
            .first { providerInfo -> providerInfo.provider == component }
    return providerInfo.generatedPreviewCategories.and(widgetCategory) != 0
}
val receiverClasses = listOf<Class<GlanceAppWidgetReceiver>>()
val glanceAppWidgetManager = GlanceAppWidgetManager(context)
withContext(Dispatchers.Default) {
    try {
        for (receiver in receiverClasses) {
            if (receiver.hasPreviewForCategory(WIDGET_CATEGORY_HOME_SCREEN)) {
                Log.i("Widget", "Skipped updating previews for $receiver")
                continue
            }
            if (
                glanceAppWidgetManager.setWidgetPreviews(receiver.kotlin) ==
                    SET_WIDGET_PREVIEWS_RESULT_RATE_LIMITED
            ) {
                Log.e("Widget", "Failed to set previews for $receiver, rate limited")
            }
        }
    } catch (e: Exception) {
        Log.e("Widget", "Error thrown when calling setWidgetPreview", e)
    }
}
```

| Parameters |
|---|---|
| `receiver: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetReceiver>` | the `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetReceiver` which holds the `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget` to run. This receiver must be registered as an app widget provider in the application manifest. |
| `widgetCategories: https://developer.android.com/reference/kotlin/androidx/collection/IntSet = intSetOf( WIDGET_CATEGORY_HOME_SCREEN or WIDGET_CATEGORY_KEYGUARD or WIDGET_CATEGORY_SEARCHBOX )` | the widget categories for which to set previews. Each element of this set must be a combination of `https://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html#WIDGET_CATEGORY_HOME_SCREEN`, `https://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html#WIDGET_CATEGORY_KEYGUARD`, or `https://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html#WIDGET_CATEGORY_SEARCHBOX`. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#SET_WIDGET_PREVIEWS_RESULT_SUCCESS()` if the preview was successfully updated. Returns `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#SET_WIDGET_PREVIEWS_RESULT_RATE_LIMITED()` when the caller has hit a system-defined rate limit on setting previews for a particular provider. In this case, you may opt to schedule a task in the future to try again, if necessary. |

## Extension functions

### GlanceAppWidgetManager.setWidgetPreviews

Artifact: [androidx.glance:glance-appwidget](https://developer.android.com/jetpack/androidx/releases/glance) [View Source](https://cs.android.com/search?q=file:androidx/glance/appwidget/GlanceAppWidgetManager.kt+function:setWidgetPreviews)

```
@RequiresApi(value = 35)
suspend inline fun <T : GlanceAppWidgetReceiver> GlanceAppWidgetManager.setWidgetPreviews(
    widgetCategories: IntSet = intSetOf(
            WIDGET_CATEGORY_HOME_SCREEN or WIDGET_CATEGORY_KEYGUARD or WIDGET_CATEGORY_SEARCHBOX
        )
): Int
```

Generate and publish the widget previews for a `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetReceiver` for the given set of `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/package-summary#(androidx.glance.appwidget.GlanceAppWidgetManager).setWidgetPreviews(androidx.collection.IntSet)`. Previews are generated from the layout provided by `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget#providePreview(android.content.Context,kotlin.Int)` on the widget connected to the given `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetReceiver` class. This receiver must be registered as an app widget provider in the application manifest.

Previews should be published during the initial setup phase or launch of your app. To avoid running this unnecessarily, you can see what previews are currently published for your provider by checking `https://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html#generatedPreviewCategories`.

The preview composition is run for each value in the `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/package-summary#(androidx.glance.appwidget.GlanceAppWidgetManager).setWidgetPreviews(androidx.collection.IntSet)` array. If your widget has the same layout across categories, you can combine all of the categories in a single value, e.g. `WIDGET_CATEGORY_HOME_SCREEN or WIDGET_CATEGORY_KEYGUARD or WIDGET_CATEGORY_SEARCHBOX`, which will call `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/package-summary#(androidx.glance.appwidget.GlanceAppWidget).composeForPreview(android.content.Context,kotlin.Int,android.appwidget.AppWidgetProviderInfo)` once and set the previews for all of the categories.

Note that this API is only available on `https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#VANILLA_ICE_CREAM` and above, so you will likely want to set `https://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html#previewLayout` and `https://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html#previewImage` as well to have the most coverage across versions.

See also `https://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html#generatedPreviewCategories`, `https://developer.android.com/reference/android/appwidget/AppWidgetManager.html#setWidgetPreview(android.content.ComponentName, int, android.widget.RemoteViews)`, `https://developer.android.com/reference/android/appwidget/AppWidgetManager.html#getWidgetPreview(android.content.ComponentName, android.os.UserHandle, int)`, and `https://developer.android.com/reference/android/appwidget/AppWidgetManager.html#removeWidgetPreview(android.content.ComponentName, int)`.

```kotlin
import androidx.glance.appwidget.GlanceAppWidgetManager
import androidx.glance.appwidget.GlanceAppWidgetManager.Companion.SET_WIDGET_PREVIEWS_RESULT_RATE_LIMITED
import androidx.glance.appwidget.GlanceAppWidgetReceiver

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM) {
    return
}
fun Class<GlanceAppWidgetReceiver>.hasPreviewForCategory(widgetCategory: Int): Boolean {
    val component = ComponentName(context, this)
    val providerInfo =
        (context.getSystemService(Context.APPWIDGET_SERVICE) as AppWidgetManager)
            .installedProviders
            .first { providerInfo -> providerInfo.provider == component }
    return providerInfo.generatedPreviewCategories.and(widgetCategory) != 0
}
val receiverClasses = listOf<Class<GlanceAppWidgetReceiver>>()
val glanceAppWidgetManager = GlanceAppWidgetManager(context)
withContext(Dispatchers.Default) {
    try {
        for (receiver in receiverClasses) {
            if (receiver.hasPreviewForCategory(WIDGET_CATEGORY_HOME_SCREEN)) {
                Log.i("Widget", "Skipped updating previews for $receiver")
                continue
            }
            if (
                glanceAppWidgetManager.setWidgetPreviews(receiver.kotlin) ==
                    SET_WIDGET_PREVIEWS_RESULT_RATE_LIMITED
            ) {
                Log.e("Widget", "Failed to set previews for $receiver, rate limited")
            }
        }
    } catch (e: Exception) {
        Log.e("Widget", "Error thrown when calling setWidgetPreview", e)
    }
}
```

| Parameters |
|---|---|
| `widgetCategories: https://developer.android.com/reference/kotlin/androidx/collection/IntSet = intSetOf( WIDGET_CATEGORY_HOME_SCREEN or WIDGET_CATEGORY_KEYGUARD or WIDGET_CATEGORY_SEARCHBOX )` | the widget categories for which to set previews. Each element of this set must be a combination of `https://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html#WIDGET_CATEGORY_HOME_SCREEN`, `https://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html#WIDGET_CATEGORY_KEYGUARD`, or `https://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html#WIDGET_CATEGORY_SEARCHBOX`. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#SET_WIDGET_PREVIEWS_RESULT_SUCCESS()` if the preview was successfully updated. Returns `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#SET_WIDGET_PREVIEWS_RESULT_RATE_LIMITED()` when the caller has hit a system-defined rate limit on setting previews for a particular provider. In this case, you may opt to schedule a task in the future to try again, if necessary. |