How to create a dynamic application shortcut using the ShortcutManager API for android 7.1 application?

In Android 7.1, a developer can create an AppShortCut .

We can create a shortcut in two ways:

  • Static shortcuts using resources (XML).
  • Dynamic shortcuts using the API ShortcutManager.

So how to create a shortcut using ShortcutManagerdynamically?

+4
source share
2 answers

Using ShortcutManager, we can create a application shortcut for a dynamic application as follows:

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>

The developer can also perform various tasks with ShortcutManager:

Application shortcut

Github

https://developer.android.com/preview/shortcuts.html ShortcutManager, .

+8

ShortcutManager ,

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

ShortcutInfo webShortcut = new ShortcutInfo.Builder(this, "shortcut_web")
        .setShortLabel("catinean.com")
        .setLongLabel("Open catinean.com web site")
        .setIcon(Icon.createWithResource(this, R.drawable.ic_dynamic_shortcut))
        .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://catinean.com")))
        .build();

shortcutManager.setDynamicShortcuts(Collections.singletonList(webShortcut));

ShortcutManager

    ShortcutInfo dynamicShortcut = new ShortcutInfo.Builder(this, "shortcut_dynamic")
        .setShortLabel("Dynamic")
        .setLongLabel("Open dynamic shortcut")
        .setIcon(Icon.createWithResource(this, R.drawable.ic_dynamic_shortcut_2))
        .setIntents(
                new Intent[]{
                        new Intent(Intent.ACTION_MAIN, Uri.EMPTY, this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
                })
        .build();
shortcutManager.setDynamicShortcuts(Arrays.asList(webShortcut, dynamicShortcut));
+5

Source: https://habr.com/ru/post/1659972/


All Articles