How can I place app icon on launcher home screen?

前端 未结 6 1496
迷失自我
迷失自我 2020-12-02 17:47

As you know, when app is nomally installed, icon is created at launcher menu screen. What I want to do is create icon at user home screen during installation. (without press

6条回答
  •  误落风尘
    2020-12-02 18:23

    You can use this:

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

    You have to use following permission in your AndroidManaifest.xml

    
    

    You can use the commented code according to your requirements.

    Note that, perhaps, above API is not documented. But it works.

提交回复
热议问题