可以通过ShortcutManagerCompat的getShortcuts()方法获取所有添加的快捷方式,然后判断是否包含指定的id即可。
matchFlags – result includes shortcuts matching this flags. Any combination of:
- FLAG_MATCH_MANIFEST
- FLAG_MATCH_DYNAMIC
- FLAG_MATCH_PINNED
- FLAG_MATCH_CACHED
Compatibility behavior:
API 30 and above, this method matches platform behavior.
API 25 through 29, this method aggregates the result from corresponding platform api.
API 24 and earlier, this method can only returns dynamic shortcut. Calling this method with other flag will be ignored.
比如通过ShortcutManagerCompat.requestPinShortcut()方法创建的就是FLAG_MATCH_PINNED类型
如:
private boolean checkHasSame(String id) {
List<ShortcutInfoCompat> shortcuts = ShortcutManagerCompat.
getShortcuts(mContext, ShortcutManagerCompat.FLAG_MATCH_PINNED); // Pin类型
if (shortcuts.isEmpty()) {
return false;
}
for (ShortcutInfoCompat shortcutInfo : shortcuts) {
String hasId = shortcutInfo.getId();
if (hasId.equals(id)) {
GZLog.d(TAG, "same pin shortcut id : " + hasId);
return true;
}
}
return false;
}