How can I place app icon on launcher home screen?

前端 未结 6 1497
迷失自我
迷失自我 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条回答
  •  猫巷女王i
    2020-12-02 18:09

    I use these methods to properly add or remove shortcuts. These methods are working pretty well and are the same as the Android System when the user manually add/remove a shortcut.

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

    Permissions :

    
    
    

提交回复
热议问题