问题
I have main activity window in my app, which displays three icons on it, also there are 3 home screen widgets in this application.
is there a possibility for long press on one of the icons in the main activity, to do the same behavior as when you install the program shortcuts from the application menu when dragging shortcuts on the desktop?(for example this video: http://www.youtube.com/watch?v=DQ37cASti4k) or the user to have to go to home screen \ menu \ add \ widget??
回答1:
The user will have to manually install the app widget. Since widgets need to be positioned -- and so users do not have app widgets forced upon them -- users must choose to add it to their home screen.
回答2:
In Android O, its possible to set app widget programmatically.
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
来源:https://stackoverflow.com/questions/4084862/install-home-screen-widgets-progrommaticly