How to create dynamic app shortcut using ShortcutM

2020-02-26 08:41发布

问题:

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?

回答1:

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:

  • Publish: Use setDynamicShortcuts(List) to redefine the entire list of dynamic shortcuts, or use addDynamicShortcuts(List) to augment an existing list of dynamic shortcuts.
  • Update: Use the updateShortcuts(List) method.
  • Remove: Remove a set of dynamic shortcuts using removeDynamicShortcuts(List), or remove all dynamic shortcuts using removeAllDynamicShortcuts().

Check Github example for App Shortcut

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



回答2:

We can use ShortcutManager for a intent action like this

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

we can use ShortcutManager to open an activity like this

    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(dynamicShortcut, dynamicShortcut));


回答3:

Perfect Solution for all devices (Pre/Post Oreo device).

createShortcut(CurrentActivity.this, ActivityToOpen.class, "app name", R.mipmap.ic_launcher);

Put this in your Util.

 public static void createShortcut(@NonNull Activity activity, Class activityToOpen, String title, @DrawableRes int icon) {
        Intent shortcutIntent = new Intent(activity, activityToOpen);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { // code for adding shortcut on pre oreo device 
            Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
            intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
            intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
            intent.putExtra("duplicate", false);
            Parcelable parcelable = Intent.ShortcutIconResource.fromContext(activity, icon);
            intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, parcelable);
            activity.sendBroadcast(intent);
            System.out.println("added_to_homescreen");
        } else { 
            ShortcutManager shortcutManager = activity.getSystemService(ShortcutManager.class);
            assert shortcutManager != null;
            if (shortcutManager.isRequestPinShortcutSupported()) {
                ShortcutInfo pinShortcutInfo =
                        new ShortcutInfo.Builder(activity, "browser-shortcut-")
                                .setIntent(shortcutIntent)
                                .setIcon(Icon.createWithResource(activity, icon))
                                .setShortLabel(title)
                                .build();

                shortcutManager.requestPinShortcut(pinShortcutInfo, null);
                System.out.println("added_to_homescreen");
            } else {
                System.out.println("failed_to_add");
            }
        }
    }


回答4:

We should set the shortcut intent to Target Activity rather than mentioning implicit intents.

Eg:

ShortcutManager shortcutManager = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
        shortcutManager =  getSystemService(ShortcutManager.class);
        Intent intent= new Intent(this, MainActivity.class);
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("weburl"));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "shortcutId")
                .setShortLabel("dummy")
                .setLongLabel("dummy").setRank(0)
                .setIcon(Icon.createWithResource(this, R.drawable.ic_launcher_background))
                .setIntent(intent).build();

         shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));
}

And Activity in my Manifest.xml is :

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
             <action android:name="android.intent.action.VIEW" />

        </intent-filter>
    </activity>

Am landing to my MainActivity on clicking the dynamic shortCut which handles Action_VIEW .