【问题标题】:programmatically add the widget to home screen in android以编程方式将小部件添加到 android 的主屏幕
【发布时间】:2020-05-02 17:04:36
【问题描述】:

我已经开发了 android 小部件应用程序并且它工作正常。 现在我的客户问,当用户安装这个应用程序时,它需要自动放置在主屏幕的顶部位置。这个怎么做?请帮帮我。

【问题讨论】:

  • 查看应用程序CM flashlight它做同样的事情,而应用程序本身的安装Widget和应用程序图标被放置在主屏幕上,我不确定CM flshlight是如何做到的,但我可以肯定地完成,但我还不知道怎么做。

标签: android widget workspace android-launcher


【解决方案1】:
  1. 创建小部件提供程序类
  2. 放入清单
  3. 点击您想添加到主页的按钮

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    AppWidgetManager mAppWidgetManager = getSystemService(AppWidgetManager.class);

    ComponentName myProvider = new ComponentName(AddWidgetActivity.this, AppWidgetSmall.class);

    Bundle b = new Bundle();
    b.putString("ggg", "ggg");
    if (mAppWidgetManager.isRequestPinAppWidgetSupported()) {
         Intent pinnedWidgetCallbackIntent = new Intent(AddWidgetActivity.this, AppWidgetSmall.class);
         PendingIntent successCallback = PendingIntent.getBroadcast(AddWidgetActivity.this, 0,
                 pinnedWidgetCallbackIntent, 0);

         mAppWidgetManager.requestPinAppWidget(myProvider, b, successCallback);
    }
}

【讨论】:

    【解决方案2】:

    从 Android O 开始,在您的应用中,您可以创建一个请求,让系统将小部件固定到支持的启动器上。

    1. 在应用的清单文件中创建小部件
    2. 调用 requestPinAddWidget() 方法

    查看本页底部:https://developer.android.com/preview/features/pinning-shortcuts-widgets.html

    【讨论】:

      【解决方案3】:

      参考http://viralpatel.net/blogs/android-install-uninstall-shortcut-example/:

      Android 为我们提供了一个意图类 com.android.launcher.action.INSTALL_SHORTCUT,它可用于向主屏幕添加快捷方式。在下面的代码 sn-p 中,我们创建了一个名为 HelloWorldShortcut 的活动 MainActivity 的快捷方式。

      首先我们需要将权限 INSTALL_SHORTCUT 添加到 android manifest xml。

      <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
      

      addShortcut() 方法在主屏幕上创建一个新的快捷方式。

      private void addShortcut() {
          //Adding shortcut for MainActivity 
          //on Home screen
          Intent shortcutIntent = new Intent(getApplicationContext(),
                  MainActivity.class);
      
          shortcutIntent.setAction(Intent.ACTION_MAIN);
      
          Intent addIntent = new Intent();
          addIntent
                  .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
          addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
          addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                  Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                          R.drawable.ic_launcher));
      
          addIntent
                  .setAction("com.android.launcher.action.INSTALL_SHORTCUT");
          getApplicationContext().sendBroadcast(addIntent);
      }
      

      请注意我们如何创建包含目标活动的快捷方式意图对象。此意图对象作为 EXTRA_SHORTCUT_INTENT 添加到另一个意图中。最后我们广播新的意图。这会添加一个名称为的快捷方式 EXTRA_SHORTCUT_NAME 和由 EXTRA_SHORTCUT_ICON_RESOURCE 定义的图标。 注意:这里值得注意的一点是,当您定义从快捷方式调用的活动时,您必须在标签中定义 android:exported=”true” 属性。

      从主屏幕卸载快捷方式:

      与 Android 中的安装、卸载或删除快捷方式类似,它使用 Intent (UNINSTALL_SHORTCUT) 来执行任务。在下面的代码中,我们删除了添加在主屏幕上的快捷方式。

      再次,我们需要执行卸载快捷方式任务的权限。向 Android 清单 xml 添加以下权限。

      <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
      

      removeShortcut() 方法与 addShortcut() 完全相反。除了 removeShortcut 调用 UNINSTALL_SHORTCUT 意图之外,大部分代码都相似。

      private void removeShortcut() {
      
          //Deleting shortcut for MainActivity 
          //on Home screen
          Intent shortcutIntent = new Intent(getApplicationContext(),
                  MainActivity.class);
          shortcutIntent.setAction(Intent.ACTION_MAIN);
      
          Intent addIntent = new Intent();
          addIntent
                  .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
          addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
      
          addIntent
                  .setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
          getApplicationContext().sendBroadcast(addIntent);
      }
      

      你可以试试这个演示HERE

      【讨论】:

      • 这会创建快捷方式,而不是小部件。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-14
      • 1970-01-01
      相关资源
      最近更新 更多