我的应用程序中有主活动窗口,上面显示三个图标,此应用程序中还有 3 个主屏幕小部件。
是否有可能长按主活动中的一个图标,以执行与在桌面上拖动快捷方式时从应用程序菜单安装程序快捷方式时相同的行为?(例如此视频: http:// www.youtube.com/watch?v=DQ37cASti4k)或用户必须转到主屏幕\菜单\添加\小部件?
我的应用程序中有主活动窗口,上面显示三个图标,此应用程序中还有 3 个主屏幕小部件。
是否有可能长按主活动中的一个图标,以执行与在桌面上拖动快捷方式时从应用程序菜单安装程序快捷方式时相同的行为?(例如此视频: http:// www.youtube.com/watch?v=DQ37cASti4k)或用户必须转到主屏幕\菜单\添加\小部件?
用户必须手动安装应用小部件。由于需要定位小部件——因此用户没有强加于他们的应用小部件——用户必须选择将其添加到他们的主屏幕。
在 Android O 中,可以通过编程方式设置应用小部件。
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());
}
另请查看 Google 官方文档