How to create dynamic app shortcut using ShortcutManager API for android 7.1 app?

pRaNaY picture pRaNaY · Nov 6, 2016 · Viewed 12.3k times · Source

In Android 7.1, developer can able to create AppShortCut.

We can create shortcut in two way:

  1. Static shortcuts using resources(XML) file.
  2. Dynamic shortcuts using ShortcutManager API.

So How to create a shortcut using ShortcutManager dynamically?

Answer

pRaNaY picture pRaNaY · Nov 6, 2016

Using ShortcutManager, we can create app dynamic app shortcut in following way:

ShortcutManager shortcutManager;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        shortcutManager = getSystemService(ShortcutManager.class);
        ShortcutInfo shortcut;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
            shortcut = new ShortcutInfo.Builder(this, "second_shortcut")
                    .setShortLabel(getString(R.string.str_shortcut_two))
                    .setLongLabel(getString(R.string.str_shortcut_two_desc))
                    .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
                    .setIntent(new Intent(Intent.ACTION_VIEW,
                            Uri.parse("https://www.google.co.in")))
                    .build();
            shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));
        }


    }

String resources:

<string name="str_shortcut_two">Shortcut 2</string>
<string name="str_shortcut_two_desc">Shortcut using code</string>

Developer can also perform different tasks app shortcut using ShortcutManager:

App Shortcut

Check Github example for App Shortcut

Check https://developer.android.com/preview/shortcuts.html and ShortcutManager to get more info.