以编程方式安装主屏幕小部件

发布于 2024-09-30 09:26:19 字数 258 浏览 0 评论 0原文

我的应用程序中有一个主活动窗口,上面显示三个图标,并且该应用程序中有 3 个主屏幕小部件。

是否可以长按主活动中的一个图标,以执行与在桌面上拖动快捷方式时从应用程序菜单安装程序快捷方式相同的行为?(例如此视频:http://www.youtube.com/watch?v=DQ37cASti4k) 或者用户必须去主屏幕\菜单\添加\小部件??

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??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一影成城 2024-10-07 09:26:19

用户必须手动安装应用程序小部件。由于小部件需要定位 - 因此用户不会强制使用应用程序小部件 - 用户必须选择将其添加到主屏幕。

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.

弥枳 2024-10-07 09:26:19

在 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 官方文档

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文