如何使用ShortcutManager API在Android 7.1应用程序中创建动态应用程序快捷方式?

10
在Android 7.1中,开发者可以创建应用快捷方式
我们可以通过两种方式创建快捷方式:
  1. 使用资源(XML)文件创建静态快捷方式。
  2. 使用ShortcutManager API创建动态快捷方式。
那么如何使用ShortcutManager动态地创建快捷方式呢?

1
AppShortcuts 简单项目:https://developer.android.com/samples/AppShortcuts/index.html - Benny
现在示例项目在这里 https://github.com/android/user-interface-samples/tree/master/AppShortcuts/#readme - Gema Sanchez
5个回答

13

通过使用ShortcutManager,我们可以按照以下方式创建应用动态快捷方式:

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 name="str_shortcut_two">Shortcut 2</string>
<string name="str_shortcut_two_desc">Shortcut using code</string>

开发者还可以使用ShortcutManager执行不同的应用程序快捷方式任务:

应用程序快捷方式

查看Github示例以获取应用程序快捷方式

请查看https://developer.android.com/preview/shortcuts.htmlShortcutManager以获取更多信息。


1
M?API 25之后才有吧? - Gokhan Arik

6
我们可以使用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来打开一个activity,如下所示。
    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

为所有设备提供完美解决方案(适用于 Oreo 之前和之后的设备)。

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

将此放入您的工具类中。

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

它会抱怨shortcutIntent没有动作。 - CodingTT
如果您想创建活动的快捷方式,那么这个答案会更好,只需要设置快捷方式意图以满足您的需求,例如在我的情况下是这样的:new Intent(applicationContext,Activity.class)。希望能帮助到正在寻找活动快捷方式的人。 - Hamza Khan

0

我们应该将快捷方式意图设置为目标活动,而不是提及隐式意图。

例如:

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

我的Manifest.xml中的Activity是:
    <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>

点击处理Action_VIEW的动态快捷方式后,我会跳转到我的MainActivity。

0
动态快捷方式: ShortcutManager 用于管理所有动态快捷方式。 ShortcutInfo 表示单个快捷方式,然后通过 ShortcutInfo.Builder 添加到 ShortcutManager 中。
   //......................................* Java Dynamic Shortcuts
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

Intent thirdIntent = new Intent(this,ThirdActivity.class);
thirdIntent.setAction(Intent.ACTION_VIEW);

ShortcutInfo thirdScreenShortcut = new ShortcutInfo.Builder(this, "shortcut_third")
 .setShortLabel("Third Activity")
 .setLongLabel("This is long description for Third Activity")
 .setIcon(Icon.createWithResource(this, R.drawable.your_image))
 .setIntent(thirdIntent)
 .build();
 shortcutManager.setDynamicShortcuts(Collections.singletonList(thirdScreenShortcut));








  //......................................*  KOTLIN Dynamic Shortcuts
 val shortcutManager = getSystemService(ShortcutManager::class.java)


            val thirdIntent = Intent(this, ActivityThree::class.java)
            thirdIntent.action = Intent.ACTION_VIEW

            val dynamicShortcut = ShortcutInfo.Builder(this, "shortcut_third")
                .setShortLabel("Third Activity")
                .setLongLabel("This is long description for Third Activity")
                .setIcon(Icon.createWithResource(this, R.drawable.your_image))
                .setIntents(
                    arrayOf(
                        Intent(
                            Intent.ACTION_MAIN,
                            Uri.EMPTY,
                            this,
                            MainActivity::class.java
                        ).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
                        thirdIntent
                    )
                )
                .build()

  shortcutManager!!.dynamicShortcuts = listOf(dynamicShortcut)

重点说明: https://androidkeynotes.blogspot.com/2020/02/app-shortcuts.html

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接