Sui dispositivi con Android 8.0 (livello API 26) e versioni successive, i lanci che consentono agli utenti di creare scorciatoie bloccate consentono anche di bloccare i widget nella schermata Home. Analogamente alle scorciatoie fissate, questi I widget bloccati consentono agli utenti di accedere ad attività specifiche nella tua app e possono essere aggiunto alla schermata Home direttamente dall'app, come mostrato in questo video.
Consentire agli utenti di fissare un widget
Nella tua app, puoi creare una richiesta al sistema per bloccare un widget su una Avvio app supportato svolgendo i seguenti passaggi:
Assicurati di dichiarare un widget nel file manifest della tua app.
Chiama il
requestPinAppWidget()
come mostrato nello snippet di codice riportato di seguito:
Kotlin
val appWidgetManager = AppWidgetManager.getInstance(context) val myProvider = ComponentName(context, ExampleAppWidgetProvider::class.java) if (appWidgetManager.isRequestPinAppWidgetSupported()) { // Create the PendingIntent object only if your app needs to be notified // when the user chooses to pin the widget. Note that if the pinning // operation fails, your app isn't notified. This callback receives the ID // of the newly pinned widget (EXTRA_APPWIDGET_ID). val successCallback = PendingIntent.getBroadcast( /* context = */ context, /* requestCode = */ 0, /* intent = */ Intent(...), /* flags = */ PendingIntent.FLAG_UPDATE_CURRENT) appWidgetManager.requestPinAppWidget(myProvider, null, successCallback) }
Java
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); ComponentName myProvider = new ComponentName(context, ExampleAppWidgetProvider.class); if (appWidgetManager.isRequestPinAppWidgetSupported()) { // Create the PendingIntent object only if your app needs to be notified // when the user chooses to pin the widget. Note that if the pinning // operation fails, your app isn't notified. This callback receives the ID // of the newly pinned widget (EXTRA_APPWIDGET_ID). PendingIntent successCallback = PendingIntent.getBroadcast( /* context = */ context, /* requestCode = */ 0, /* intent = */ new Intent(...), /* flags = */ PendingIntent.FLAG_UPDATE_CURRENT); appWidgetManager.requestPinAppWidget(myProvider, null, successCallback); }
Linee guida per la progettazione correlate
Gli utenti scoprono e aggiungono il widget tramite il selettore widget o dall'interno app quando la funzionalità del widget è più pertinente. Per ulteriori informazioni, vedi Scoperta e promozione.