简体   繁体   English

Android ShortCutManager 将 static 快捷方式替换为动态快捷方式

[英]Android ShortCutManager replaces static shortcut by dynamic one

I have 4 static shortcuts defined im my shortcuts.xml (static1, static2, static3, static4) - all are enabled.我在我的快捷方式中定义了 4 个 static 快捷方式。xml(static1、static2、static3、static4)-全部启用。 As soon as I add a dynamic shortcut (dynamic1), the last static shortcut (static4) is overridden by the dynamic one.一旦我添加动态快捷方式 (dynamic1),最后一个 static 快捷方式 (static4) 就会被动态快捷方式覆盖。 Thereby, the launcher now displays static1, static2, static3, static4 instead of static1, static2, static3, static4 .因此,启动器现在显示static1, static2, static3, static4而不是static1, static2, static3, static4 When I add a second dynamic shortcut (dynamic2) it overrides the static3 as well.当我添加第二个动态快捷方式 (dynamic2) 时,它也会覆盖 static3。

In the ShortcutManager documentation it is written clearly that static shortcuts are always ranked higher (rank == 0) than dynamic ones and thereby, should be shown before dynamic ones.在 ShortcutManager 文档中明确指出 static 快捷方式的排名始终高于动态快捷方式 (rank == 0),因此应该在动态快捷方式之前显示。

https://developer.android.com/guide/topics/ui/shortcuts/managing-shortcuts#display-order https://developer.android.com/guide/topics/ui/shortcuts/managing-shortcuts#display-order

When the launcher displays an app's shortcuts, they should appear in the following order:当启动器显示应用程序的快捷方式时,它们应按以下顺序显示:

  1. Static shortcuts: Shortcuts whose isDeclaredInManifest() method returns true. Static 快捷方式:isDeclaredInManifest() 方法返回 true 的快捷方式。
  2. Dynamic shortcuts: Shortcuts whose ShortcutInfo.isDynamic() method returns true.动态快捷方式: ShortcutInfo.isDynamic() 方法返回 true 的快捷方式。 Within each shortcut type (static and dynamic), shortcuts are sorted in order of increasing rank according to ShortcutInfo.getRank().在每种快捷方式类型(静态和动态)中,快捷方式根据 ShortcutInfo.getRank() 按排名递增的顺序进行排序。

I'm not sure what is going wrong here and how I can fix it to always show the static shortcuts.我不确定这里出了什么问题以及如何修复它以始终显示 static 快捷方式。 The purpose of my dynamic shortcuts is to provide direct share functionality.我的动态快捷方式的目的是提供直接共享功能。

Here is how I add my dynamic ShortCut(s):这是我添加动态快捷方式的方法:

    final ArrayList<ShortcutInfoCompat> shortcuts = new ArrayList<>();
    final Set<String> categories = Collections.singleton("my.package.category.SOME_CATEGORY");

    final ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(context, "shortcutId")
            .setShortLabel("some_label")
            .setIcon(IconCompat.createWithResource(context, R.drawable.some_icon))
            .setIntent(new Intent(Intent.ACTION_DEFAULT))
            .setCategories(categories)
            .setPerson(
                    new Person.Builder()
                            .setName("some_name")
                            .build()
            )
            .build();

    shortcuts.add(shortcut);

    ShortcutManagerCompat.addDynamicShortcuts(context, shortcuts);

While it's true that static shortcuts are always before the dynamic ones, from reading the documentation, there seems to be no guarantee on the amount of shortcuts that are displayed on each group.虽然 static 快捷方式确实总是在动态快捷方式之前,但从阅读文档来看,似乎无法保证每个组上显示的快捷方式数量。

There are several factors you should consider from the documentation:您应该从文档中考虑几个因素:

Shortcut limitations 快捷方式限制

Although you can publish up to five shortcuts (static and dynamic shortcuts combined) at a time for your app, most launchers can only display four.虽然您一次最多可以为您的应用程序发布五个快捷方式(静态和动态快捷方式的组合),但大多数启动器只能显示四个。

So what should android do when you add a fifth dynamic shortcut in addition to the four static ones?那么,当您在 static 的四个动态快捷方式之外添加第五个动态快捷方式时,android 应该做什么呢? You'd never be able to show the dynamic one to the user if all the static ones are displayed.如果显示所有 static 的,您将永远无法向用户显示动态的。

Shortcut count limitation 快捷方式计数限制

Each app's launcher icon can contain at most getMaxShortcutCountPerActivity() number of static and dynamic shortcuts combined.每个应用程序的启动器图标最多可以包含getMaxShortcutCountPerActivity()数量 static 和组合的动态快捷方式。 There is no limit to the number of pinned shortcuts that an app can create, though.不过,应用程序可以创建的固定快捷方式的数量没有限制。

So watch out for that maximum, as you won't be able to add more than that anyways.所以要注意这个最大值,因为无论如何你都不能添加更多。

Best practices 最佳实践

Publish only four distinct shortcuts仅发布四个不同的快捷方式
Although the API currently supports a combination of up to five static and dynamic shortcuts for your app at any given time, we recommend that you publish only four distinct shortcuts to improve their visual appearance in the launcher.尽管 API 目前在任何给定时间支持最多五个 static 和动态快捷方式的组合,但我们建议您仅发布四个不同的快捷方式以改善它们在启动器中的视觉外观。


I can't tell you what you should do, but having that many static and dynamic shortcuts is simply not possible. 我不能告诉你应该做什么, 但是拥有那么多 static 和动态快捷方式是根本不可能的。 While it is possible to have that many shortcuts, it's up to the launcher to choose what to display.虽然可以有那么多快捷方式,但要由启动器选择要显示的内容。 Therefore you should consider the recommended maximum of 4. You may reconsider what shortcuts you deem essential and which ones you do not.因此,您应该考虑建议的最大值 4。您可以重新考虑哪些快捷方式是您认为必不可少的,哪些不是。

Another approach may be to use Pinned Shortcuts as an alternative.另一种方法可能是使用固定快捷方式作为替代方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM