如何将应用程序图标放置在启动器主屏幕上?

21

正如您所知道的那样,当应用程序被正常安装时,图标会在启动器菜单屏幕上创建。

我想做的是在安装过程中在用户主屏幕上创建图标。 (无需按下图标5秒钟)

我从另一个来源听说只需添加以下内容即可:

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

我将权限添加到AndroidManifest.xml文件中,但没有生效。

是否还有其他方法可行?


13
您不觉得这样做可能是一种不好的做法吗?如果用户想要在屏幕上显示图标,他们可以非常容易地自己完成。 - Jems
3
这就像那些会让人发怒的 Windows 应用程序一样,它们坚持在每次安装/升级/启动时将自己添加到桌面上。即使你找到了方法,请不要这样做。 - ZoogieZork
6
@Jems,ZoogieZork 你们两个都是正确的,但某些情况下需要这种功能,例如企业应用商店仅对公司拥有的设备上的员工开放。许多用户将不具备将应用程序放置在主屏幕上的知识,但如果我们为他们完成这项任务,他们会获得更好的用户体验。 - Martin
2
此外,一些用于开发多个应用程序并需要将它们全部加载到手机上的企业所使用的设备可能需要进行大量测试。在测试之前,他们需要将应用程序加载到设备上并将应用程序放置在主屏幕上,为了使这一过程更加简便,自动将应用程序添加到主屏幕上会是一个不错的选择,可以节省大量时间。 - prolink007
9
我不同意这是不良习惯。我与许多用户交谈后得知,他们希望图标出现后自行决定如何处理它:移动,删除或保留...很多用户在安装完应用程序后找不到其图标时感到困扰,在应用程序菜单中查找显然不太直观...由于主屏幕的空间有限且宝贵,用户不会犹豫地重新排列或删除图标以适应其需求(更加直观)。这与Windows桌面不同。 - Wytze
6个回答

18
您可以使用以下代码:
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);

你需要在AndroidManifest.xml中使用以下权限:

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

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

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


3
要卸载快捷方式,请参见我的回答https://dev59.com/6U_Ta4cB1Zd3GeqPDsN2。 - Vasu
实际上,作为Java的新手,我只能从Kaillash的卸载示例中理解这段代码。该示例显示您可以将其放置在主活动中。 不过,将此代码放置在onCreate()事件处理程序中似乎会导致快捷方式在每次启动应用程序时被创建/重新创建。有什么解决方法吗? - Wytze
2
它可以工作。但我认为选项:intent.putExtra(“duplicate”,false); 应默认打开,以避免在主屏幕上出现重复图标。 - anticafe
1
快捷方式图标应该与启动器图标相同吗?如果是的话,我的启动器图标放在mimp文件夹中而不是drawable文件夹中,那么我应该从mimp文件夹获取快捷方式图标资源吗? - Beeing Jk
它在我的三星S7 Edge和诺基亚8上都没有运行。我将此代码添加到MainActivity中的onCreate方法中(该方法扩展了AppCompatActivity),我用getApplicationContext()替换了context,用相应的包名替换了包名,并用“MainActivity”替换了类名。需要帮助。 - YoussefDir

9
现在,Google Play服务已经解决了这个问题。您不需要再添加任何代码来解决它。现在,当您从Google Play商店安装应用程序时,它会自动在主屏幕上创建标志。可以在Google Play商店设置中处理它。 例外情况:如果您正在使用任何自定义ROM或启动器,则无法正常工作。

实际上,这是 Android 从一开始就具备的基本功能 - 它与 Play 商店或其服务无关。 - Chris Stratton
如果你不想要它怎么办?我有一个应用程序,只能从另一个应用程序的Intent中启动。但是我有一个设置的启动器活动。我不希望默认情况下将设置图标添加到主屏幕上。 - Roel

7

我使用以下方法来正确添加或删除快捷方式。这些方法非常有效,并且与用户手动添加/删除快捷方式时的安卓系统相同

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”的类中实现这个新的广播接收器吗?请解释一下。 - YoussefDir
广播接收器是可选的,当您的应用程序包被替换时,取决于您是否想要执行某些操作。抱歉,这不太清楚。 - ChristopheCVB

2

为所有设备提供强大的解决方案(包括<26>26)。

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

这个方法可以放在您的工具类中。

 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");
            }
        }
    }

1
shortcutIntent.setAction(Intent.ACTION_MAIN); - sss

2

我对以上答案感到困惑,因为API 22和其他API在debug / emulation模式下返回getApplicationInfo()。className:
"com.android.tools.fd.runtime.BootstrapApplication"

最重要的是,我需要为类名设置整个路径。 (请参见更改)

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

Kailash答案中,我需要更改此行:

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

to

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

ChristopheCVBs的答案中,我需要更改这一行:
ComponentName comp = new ComponentName(appInfo.packageName, appInfo.className);

为了

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

-1
你可以创建一个 setup 函数,让用户可以使用它来执行所有可能的操作。例如,如果必要的话,创建任何必要的用户可访问文件夹(如果它们不存在),将快捷方式添加到主屏幕中,和/或加载初始数据。
这是我现在正在编写的一个函数,希望它能够成功。但是,重点是将其从 onCreate() 中排除,以便它不会每次都被调用!

@Wytze:这是对你在Kailash的回答中最后一条评论的回应。 - Steven_BDawg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接