Bloccare i widget Glance in-app
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Con Android 8.0 (livello API 26) e versioni successive, puoi consentire agli utenti di bloccare i tuoi
widget nella schermata Home all'interno della tua app. La promozione dei widget direttamente all'interno
della tua app è un ottimo modo per aumentare il coinvolgimento degli utenti, soprattutto dopo che un
utente completa un'attività correlata o quando un utente accede ripetutamente a una funzionalità
della tua app.
Creare una richiesta di PIN
Per avviare il blocco dei widget, utilizza il metodo requestPinGlanceAppWidget
dalla classe GlanceAppWidgetManager
. Per le app in esecuzione su versioni precedenti
di Android, viene restituito false. Tuttavia, se la richiesta viene inviata correttamente
al sistema, viene restituito il valore true.
Ecco un esempio di come creare una richiesta di PIN:
@Composable
fun AnInAppComposable() {
val context = LocalContext.current
val coroutineScope = rememberCoroutineScope()
Button(
onClick = {
coroutineScope.launch {
GlanceAppWidgetManager(context).requestPinGlanceAppWidget(
receiver = MyWidgetReceiver::class.java,
preview = MyWidget(),
previewState = DpSize(245.dp, 115.dp)
)
}
}
) {}
}
In questo esempio, MyWidgetReceiver
è la classe che riceve i callback del widget e MyWidget
è il widget Glance che vuoi bloccare. successCallback
è un PendingIntent
che viene attivato quando il widget viene
aggiunto correttamente.
Gestire la risposta alla richiesta di PIN
Quando un utente risponde alla finestra di dialogo di richiesta del PIN, la tua app riceve una
risposta. Se l'utente accetta la richiesta, il widget viene bloccato nella schermata Home e viene attivato l'intent successCallback
PendingIntent
. Se l'utente rifiuta la richiesta, non succede nulla.
È importante notare che l'successCallback
viene attivato solo se il
widget viene aggiunto correttamente alla schermata Home. Non viene attivato se l'utente
rifiuta la richiesta o se il launcher non supporta il blocco.
I campioni di contenuti e codice in questa pagina sono soggetti alle licenze descritte nella Licenza per i contenuti. Java e OpenJDK sono marchi o marchi registrati di Oracle e/o delle sue società consociate.
Ultimo aggiornamento 2025-08-26 UTC.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Mancano le informazioni di cui ho bisogno","missingTheInformationINeed","thumb-down"],["Troppo complicato/troppi passaggi","tooComplicatedTooManySteps","thumb-down"],["Obsoleti","outOfDate","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Problema relativo a esempi/codice","samplesCodeIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-08-26 UTC."],[],[],null,["With Android 8.0 (API level 26) and higher, you can let users pin your\nwidgets to their home screen within your app. Promoting widgets directly within\nyour app is a great way to increase user engagement, especially after a\nuser completes a related task, or when a user repeatedly accesses a feature in\nyour app.\n\nCreate a Pin Request\n\nTo initiate widget pinning, use the [`requestPinGlanceAppWidget`](/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#requestPinGlanceAppWidget(java.lang.Class,androidx.glance.appwidget.GlanceAppWidget,kotlin.Any,android.app.PendingIntent)) method\nfrom the [`GlanceAppWidgetManager`](/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager) class. For apps running on lower versions\nof Android, this returns false. However if the request is successfully sent\nto the system, this returns true.\n\nHere is an example of how you can create a pin request:\n\n\n```kotlin\n@Composable\nfun AnInAppComposable() {\n val context = LocalContext.current\n val coroutineScope = rememberCoroutineScope()\n Button(\n onClick = {\n coroutineScope.launch {\n GlanceAppWidgetManager(context).requestPinGlanceAppWidget(\n receiver = MyWidgetReceiver::class.java,\n preview = MyWidget(),\n previewState = DpSize(245.dp, 115.dp)\n )\n }\n }\n ) {}\n}https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/glance/GlancePinAppWidget.kt#L44-L59\n```\n\n\u003cbr /\u003e\n\nIn this example, `MyWidgetReceiver` is the class that receives the widget's\ncallbacks, and `MyWidget` is the Glance widget you want to pin. The\n`successCallback` is a `PendingIntent` that is triggered when the widget is\nsuccessfully pinned.\n\nHandle the Pin Request Response\n\nWhen a user responds to the pin request dialog, your app receives a\nresponse. If the user accepts the request, your widget is pinned to their\nhome screen, and the `successCallback` `PendingIntent` is triggered. If the\nuser denies the request, nothing happens.\n\nIt is important to note that the `successCallback` is only triggered if the\nwidget is successfully added to the home screen. It is not triggered if the user\ndenies the request or if the launcher does not support pinning."]]