Create Widget on Application Installation
I was just wondering, is there any way I can make an android widget show up on 开发者_StackOverflowa user's home screen when they install my application? Also, can I let them choose to create a widget from within my application?
I was just wondering, is there any way I can make an android widget show up on a user's home screen when they install my application?
No, sorry. None of your code gets executed upon install.
Also, can I let them choose to create a widget from within my application?
No, sorry. Only the home screen can add app widgets to the home screen.
Where is no way to get callback upon installation. But in Android O you can pin your widget on first launch of your application.
AppWidgetManager mAppWidgetManager =
context.getSystemService(AppWidgetManager.class);
AppWidgetProviderInfo myWidgetProviderInfo = new AppWidgetProviderInfo();
ComponentName myProvider = myWidgetProviderInfo.provider;
if (mAppWidgetManager.isRequestPinAppWidgetSupported()) {
// Create the PendingIntent object only if your app needs to be notified
// that the user allowed the widget to be pinned. Note that, if the pinning
// operation fails, your app isn't notified.
Intent pinnedWidgetCallbackIntent = new Intent( ... );
// Configure the intent so that your app's broadcast receiver gets
// the callback successfully. This callback receives the ID of the
// newly-pinned widget (EXTRA_APPWIDGET_ID).
PendingIntent successCallback = PendingIntent.createBroadcast(context, 0,
pinnedWidgetCallbackIntent);
mAppWidgetManager.requestPinAppWidget(myProvider, null,
successCallback.getIntentSender());
}
Also check out Google official documentation
精彩评论