java - 使用快捷方式管理器复制固定的快捷方式

标签 java android android-8.0-oreo android-shortcut android-shortcutmanager

我正在尝试使用 ShortcutManager 在主屏幕上创建固定快捷方式。我可以使用以下代码创建固定快捷方式:

Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("www.google.com"));
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)){
    ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
    .setIntent(i)                
    .setShortLabel("label")                  
    .setIcon(IconCompat.createWithResource(context, R.drawable.ic_launcher))
    .build();

   ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
}else{
    L.v("Shortcut", "Pinned shortcuts are not supported!");
}

我面临两个问题:

  1. 没有检查来处理重复的快捷方式。每次我单击创建快捷方式的按钮时,它都会创建一个快捷方式,并且主屏幕会被这些快捷方式填满。有什么方法可以检查快捷方式是否已经存在,例如:-
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("www.google.com"));

Intent installer = new Intent();        installer.putExtra("android.intent.extra.shortcut.INTENT", i);          installer.putExtra("android.intent.extra.shortcut.NAME", "Shortcut name");          installer.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(getApplicationContext() , R.drawable.ic_launcher));
installer.putExtra("duplicate", false);
installer.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(installer);

这段代码的问题是它在 android 8.0 及更高版本中不起作用,但它使用以下代码正确处理快捷方式的重复:-

installer.putExtra("duplicate", false);

我想使用快捷方式管理器实现相同的目标

  • 使用快捷方式管理器创建快捷方式时,图标会重复,如下所示
  • duplicate icon

    我已经查看了此处提供的解决方案,但到目前为止还没有运气:-

    Strange app icon duplication in pinned shortcut (Android O)

    有什么想法吗?

    最佳答案

    您可以通过调用获取当前所有快捷方式

    List<ShortcutInfo> currPinned = shortcutManager.getPinnedShortcuts();

    然后添加到MapSet并迭代它们,如果它已经存在,则不要再次添加

    if (currPinned != null) { for (ShortcutInfo shortcut: currPinned) { currPinnedMap.put(shortcut.getId(), shortcut); } } .... //iterate over you "new shortcuts" and check if the present already if (currPinnedMap.containsKey(id)) { continue; } // add really new ones

    关于java - 使用快捷方式管理器复制固定的快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56143655/

    相关文章:

    android - 如何在 Android Oreo 中管理来自未知来源的安装?

    java - Swing 中的规划 View 组件

    java - 我如何使用 Hibernate/JPA 在插入/更新/删除之前告诉数据库用户是谁?

    android - Appcelerator Label宽高判断

    android - 我们如何离开画中画模式?

    Android O 自动位置建议功能(又名自动填充),如何关闭

    java - 使用CXF集成jaxb绑定(bind)文件生成基于WSDL的客户端

    java - 关于如何在 Java swing 和 Awt 中绘制此布局的任何提示

    java - BuildConfig 是公共(public)的,应在名为 BuildConfig.java 的文件中声明

    android - 保存 EditText 和按钮点击的状态