Android create shortcuts on the home screen

前端 未结 10 738
忘掉有多难
忘掉有多难 2020-11-22 13:16

What I want to do is:

1) I\'m inside an activity, there are 2 buttons. If I click the first one a shortcut is created in my home screen. The shortcut open an

相关标签:
10条回答
  • 2020-11-22 13:35
    public static void addShortcutToHomeScreen(Context context)
    {
        if (ShortcutManagerCompat.isRequestPinShortcutSupported(context))
        {
            ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
                    .setIntent(new Intent(context, YourActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
                    .setShortLabel("Test")
                    .setIcon(IconCompat.createWithResource(context, R.drawable.ic_launcher))
                    .build();
            ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
        }
        else
        {
            // Shortcut is not supported by your launcher
        }
    }
    

    I've used it, but some devices on the home screen adds 2 icons. I did not understand??

    0 讨论(0)
  • 2020-11-22 13:41

    I improved a little bit a solution above. Now it saves in preferences whether a shortcut was already added and doesn't add it in new launches of an app if user deleted it. This also saves a little bit time, since the code to add an existing shortcut doesn't run anymore.

    final static public String PREFS_NAME = "PREFS_NAME";
    final static private String PREF_KEY_SHORTCUT_ADDED = "PREF_KEY_SHORTCUT_ADDED";
    
    
    // Creates shortcut on Android widget screen
    private void createShortcutIcon(){
    
        // Checking if ShortCut was already added
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        boolean shortCutWasAlreadyAdded = sharedPreferences.getBoolean(PREF_KEY_SHORTCUT_ADDED, false);
        if (shortCutWasAlreadyAdded) return;
    
        Intent shortcutIntent = new Intent(getApplicationContext(), IntroActivity.class);
        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, "YourAppName");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(addIntent);
    
        // Remembering that ShortCut was already added
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(PREF_KEY_SHORTCUT_ADDED, true);
        editor.commit();
    }
    
    0 讨论(0)
  • 2020-11-22 13:44

    Starting from Android O, this is the way to create a shortcut:

    if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
        final ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
    
        ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(context, shortcutId)
                .setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher))
                .setShortLabel(label)
                .setIntent(new Intent(context, MainActivity.class).setAction(Intent.ACTION_MAIN))
                .build();
        shortcutManager.requestPinShortcut(pinShortcutInfo, null);
    }
    

    It has a lot of limitations, sadly:

    1. Requires the user to accept adding it.
    2. Can't be added/removed in the background.
    3. Won't be removed if the targeted app is removed. Only the one which created it.
    4. Can't create the icon based on a resource of the targeted app, except if it's of the current app. You can do it from a bitmap though.

    For pre-Android O, you can use ShortcutManagerCompat to create new shortcuts too, without any of those limitations.

    0 讨论(0)
  • 2020-11-22 13:47

    The com.android.launcher.action.INSTALL_SHORTCUT broadcast no longer has any effect since android oreo. LINK

    If you want to support all android versions, especially android 8.0 or oreo and newer, use the code below to create shortcut:

    public static void addShortcutToHomeScreen(Context context)
    {
        if (ShortcutManagerCompat.isRequestPinShortcutSupported(context))
        {
            ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
                    .setIntent(new Intent(context, YourActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
                    .setShortLabel("Test")
                    .setIcon(IconCompat.createWithResource(context, R.drawable.ic_launcher))
                    .build();
            ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
        }
        else
        {
            // Shortcut is not supported by your launcher
        }
    }
    

    Add permission in manifest file:

    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
    
    0 讨论(0)
提交回复
热议问题