从Android O开始,可以创建固定的快捷方式,在支持的启动器上显示一个对话框以确认它们的创建:
ShortcutInfoCompat pinShortcutInfo = new ShortcutInfoCompat.Builder(context, uniqueShortcutId)
.setShortLabel(label)
.setIntent(shortcutIntent)
.setLongLabel(label)
.setIcon(IconCompat.createWithBitmap(bitmap))
.build();
ShortcutManagerCompat.requestPinShortcut(this, pinShortcutInfo , null);
Run Code Online (Sandbox Code Playgroud)
文档:
https://developer.android.com/reference/android/content/pm/ShortcutManager.html https://developer.android.com/guide/topics/ui/shortcuts.html
有时,固定的快捷方式不再相关.例如,它指向不再存在的东西.
在这种情况下,我希望能够删除它.
我认为这可能是由下一个代码实现的,但事实并非如此,因为它可能是关于动态快捷方式的,这是其他的东西:
ShortcutManager shortcutManager = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);
final List<ShortcutInfo> pinnedShortcuts = shortcutManager.getPinnedShortcuts();
final ArrayList<String> shortcutIdsToRemove = new ArrayList<>();
for (ShortcutInfo pinnedShortcut : pinnedShortcuts) {
final Intent pinnedShortcutIntent = pinnedShortcut.getIntent();
shortcutIdsToRemove.add(pinnedShortcut.getId());
}
shortcutManager.removeDynamicShortcuts(shortcutIdsToRemove);
// this also doesn't work : shortcutManager.disableShortcuts(shortcutIdsToRemove);
Run Code Online (Sandbox Code Playgroud)
如何删除固定快捷方式?它甚至可能吗?
更新:似乎不可能,正如谷歌在这里提到的那样.
到目前为止,这个API对我来说似乎非常有限,因为它有很多缺点:
那么,我现在的问题是:
有没有办法使用adb命令使用以前的API创建和删除快捷方式,如果需要,可以使用root?
是否可以使用adb命令删除特定的固定快捷方式,如果需要可以使用root?
如何在Android中的应用程序快捷方式图标中删除徽章?当我以编程方式创建应用程序快捷方式时,连同为快捷方式指定的图标一样,应用程序图标位于图标的右下角。我不要那个徽章
这是我使用的代码
public static void addShortcutToHomeScreen(Context context)
{
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context))
{
ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
.setIntent(new Intent(context, Splash.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
.setShortLabel("Test")
.setIcon(IconCompat.createWithResource(context, R.drawable.logo))
.build();
ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
}
else
{
// Shortcut is not supported by your launcher
}
}
Run Code Online (Sandbox Code Playgroud)