【问题标题】:Install home screen widgets progrommaticly以编程方式安装主屏幕小部件
【发布时间】:2010-11-03 07:07:28
【问题描述】:

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

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

【问题讨论】:

    标签: android android-widget homescreen


    【解决方案1】:

    用户必须手动安装应用小部件。由于小部件需要被定位——因此用户不会将应用小部件强加于他们——用户必须选择将其添加到他们的主屏幕。

    【讨论】:

    • 但是如何为他们提供将小部件从应用程序拖到主屏幕的选项?
    • @AdamArold:AFAIK,这是不可能的,抱歉。主屏幕是唯一可以启动此类操作的屏幕。
    【解决方案2】:

    在 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());
    }
    

    另请查看谷歌官方documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多