简体   繁体   中英

Android api29 How to automatically add app icon on the home screen at install?

I would like my app to automatically add the launch icon on the home screen at installation.

What I've tried already:

addShortcut method in ActivityMain

private void addShortcut() {

    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_background));

    addIntent
            .setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    addIntent.putExtra("duplicate", false);
    getApplicationContext().sendBroadcast(addIntent);
}

AndroidManifest.xml

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

Unfortunately, it doesn't work and I think this method is a bit out of date? Thank You!

It is not possible to run code in direct response to your own app being installed the first time - only once the user opens your app. However, if the user installs your app from Google Play, it will generally add the icon to the home screen by default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM