【问题标题】:How can I place app icon on launcher home screen?如何在启动器主屏幕上放置应用程序图标?
【发布时间】:2011-06-18 18:13:33
【问题描述】:

如您所知,当应用程序正常安装时,图标会在启动器菜单屏幕上创建。 我想要做的是在安装过程中在用户主屏幕上创建图标。 (不按图标 5 秒。)

我从另一个来源听到这个只是添加

<category android:value="android.intent.category.HOME" />

到 AndroidManifest.xml 文件,但它不起作用。

还有其他方法吗?

【问题讨论】:

  • 您不认为这可能是一种不好的做法吗?如果用户想要在他们的屏幕上显示图标,他们可以很容易地自己完成。
  • 这类似于那些在每次安装/升级/启动时坚持将自己添加到桌面的 Windows 应用程序。即使你找到了办法,请不要这样做
  • @Jems,ZoogieZork 你说的都对,但某些情况下需要此功能,例如,企业应用商店仅适用于公司拥有的设备上的员工。这些用户中的很多人不具备将应用程序放在主屏幕上的知识,但如果我们为他们这样做,将会获得更好的用户体验。
  • 此外,一些用于开发多个应用程序并需要将它们全部加载到手机上的企业的设备可能需要进行大量测试。在测试之前,他们需要将应用程序加载到设备上并将应用程序放在主屏幕上,为了使这更容易,最好将应用程序自动添加到主屏幕上。节省大量时间。
  • 我不同意这是不好的做法。我交谈过的许多用户都希望图标出现,然后自己决定如何处理它:移动它,或者丢弃它,或者离开它......许多用户在安装应用程序时感到很恼火,并且找不到之后在他们的主屏幕上显示它的图标。在他们的应用程序菜单中找到它至少可以说是违反直觉的......由于主屏幕很小且很有价值,用户会毫不犹豫地重新排列或转储图标以满足他们的需求(更直观)。这与 Windows 桌面不同。

标签: android


【解决方案1】:

你可以用这个:

Intent shortcutIntent = new Intent();
shortcutIntent.setClassName("packageName", "className");
//shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "shortcut_name");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));
//intent.putExtra("duplicate", false);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
            context.sendBroadcast(addIntent);

您必须在您的 AndroidManaifest.xml 中使用以下权限

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

您可以根据自己的要求使用注释代码。

请注意,可能没有记录上述 API。但它有效。

【讨论】:

  • 卸载快捷方式见我的回答stackoverflow.com/questions/3480287/…
  • 实际上,作为Java 新手,我只能从Kailash 的卸载示例中理解这段代码。该示例显示您可以将其放在主要活动中。不过,似乎将此代码放在 onCreate() 事件处理程序中会导致每次启动应用程序时都创建/重新创建快捷方式。有什么解决办法吗?
  • 它有效。但我认为选项:intent.putExtra("duplicate", false);应该默认打开以避免主屏幕上的重复图标
  • 快捷方式图标应该和启动器图标一样吗?如果是,我的启动器图标放在 mimp 文件夹而不是 drawable 文件夹中,我应该从 mimp 文件夹中获取快捷方式图标资源吗?
  • 它在我的三星 S7 edge 和诺基亚 8 上都不起作用。我在 MainActivity 的 onCreate 中添加了这段代码(它扩展了 AppCompatActivity),我用 getApplicationContext() 替换了上下文,我替换了包名由对应,类名由“MainActivity”。任何帮助。
【解决方案2】:

现在,Google Play 服务解决了这个问题。您不必再添加任何代码来执行此操作。现在,当您从 Google Play 商店安装应用程序时,它会自动在主屏幕中创建徽标。它可以在 Google Play 商店设置中处理。 例外:如果您使用任何自定义 rom 或启动器,它不适用于某些。

【讨论】:

  • 实际上,这在 Android 上从一开始就是基本功能——它与 Play 商店或其服务无关。
  • 不想要怎么办?我有一个应用程序,它只能从另一个应用程序的 Intent 启动。但我有一个用于设置的启动器活动。我不希望默认情况下将设置图标添加到主屏幕。
【解决方案3】:

我使用这些方法来正确添加或删除快捷方式。这些方法运行良好,在用户手动添加/删除快捷方式时与 Android 系统相同

public static void addShortcut(Context context)
{
    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

    ApplicationInfo appInfo = context.getApplicationInfo();

    // Shortcut name
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, appInfo.name);
    shortcut.putExtra("duplicate", false); // Just create once

    // Setup activity shoud be shortcut object 
    ComponentName component = new ComponentName(appInfo.packageName, appInfo.className);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(component));

    // Set shortcut icon
    ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(context, appInfo.icon);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

    context.sendBroadcast(shortcut);
}

public static void deleteShortcut(Context context)
{
    Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");

    ApplicationInfo appInfo = context.getApplicationInfo();

    // Shortcut name
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, appInfo.name);

    ComponentName comp = new ComponentName(appInfo.packageName, appInfo.className);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));

    context.sendBroadcast(shortcut);
}

权限:

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

【讨论】:

  • 广播接收器有必要吗?也许 addShortcut 和 deleteShortcut 向 com.android.launcher.action... 包发送广播,那么为什么我们需要广播接收器?我们是否需要在名为“PackageReplacedReceiver”的类(扩展广播接收器)中实现这个新的广播接收器?请解释一下。
  • 广播接收器当然是可选的,这取决于你是否想在你的包被替换时做些什么。抱歉,不清楚。
【解决方案4】:

我对上述答案有疑问,这是因为 API 22 和其他 API 在调试/仿真模式下返回 getApplicationInfo().className:
"com.android.tools.fd.runtime.BootstrapApplication"

最重要的是我需要为类名设置类的整个路径。 (查看更改)

例如,我有以下内容:
包名=com.example.user.myapp
className=MainActivity

Kailash answer 我需要更改这一行:

shortcutIntent.setClassName("packageName", "className");

shortcutIntent.setClassName("com.example.user.myapp", "com.example.user.myapp.MainActivity");



ChristopheCVBs answer 我需要更改这一行:

ComponentName comp = new ComponentName(appInfo.packageName, appInfo.className);

ComponentName comp = new ComponentName(appInfo.packageName, appInfo.packageName+".MainActivity");

【讨论】:

    【解决方案5】:

    适用于所有设备(&lt;26&gt;26)的稳健解决方案。

    createShortcutOnHome(CurrentActivity.this, ActivityToOpen.class, "app name", R.mipmap.ic_launcher);
    

    这个方法可以放在你的Util中。

     public static void createShortcutOnHome(@NonNull Activity activity, Class activityToOpen, String title, @DrawableRes int icon) {
            Intent shortcutIntent = new Intent(activity, activityToOpen);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { // code for adding shortcut on pre oreo device 
                Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
                intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
                intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
                intent.putExtra("duplicate", false);
                Parcelable parcelable = Intent.ShortcutIconResource.fromContext(activity, icon);
                intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, parcelable);
                activity.sendBroadcast(intent);
                System.out.println("added_to_homescreen");
            } else { 
                ShortcutManager shortcutManager = activity.getSystemService(ShortcutManager.class);
                assert shortcutManager != null;
                if (shortcutManager.isRequestPinShortcutSupported()) {
                    ShortcutInfo pinShortcutInfo =
                            new ShortcutInfo.Builder(activity, "browser-shortcut-")
                                    .setIntent(shortcutIntent)
                                    .setIcon(Icon.createWithResource(activity, icon))
                                    .setShortLabel(title)
                                    .build();
    
                    shortcutManager.requestPinShortcut(pinShortcutInfo, null);
                    System.out.println("added_to_homescreen");
                } else {
                    System.out.println("failed_to_add");
                }
            }
        }
    

    【讨论】:

    • shortcutIntent.setAction(Intent.ACTION_MAIN);
    【解决方案6】:

    您可以创建一个setup 函数,用户可以使用它来执行所有潜在操作。例如,创建任何必要的用户可访问文件夹(如果它们尚不存在)、将快捷方式添加到主屏幕和/或加载初始数据。

    这是我现在实际编写的一个函数,所以希望结果很好。但是,是的,关键是要让它远离onCreate(),这样它就不会每次都被调用!

    【讨论】:

    • @Wytze:这是对您对 Kailash 回答的最后评论的回答。
    猜你喜欢
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多