【问题标题】:Remove badge from custom android icon从自定义 android 图标中删除徽章
【发布时间】:2021-05-10 18:12:51
【问题描述】:

我正在创建一个用于创建图标的应用程序,例如Ume Icon Changer,现在我正在使用此代码来创建一个图标

    public static void createShortCut3(Context context, Drawable drawable, String shortcutname, String packagename){
    Intent intent = context.getPackageManager().getLaunchIntentForPackage(packagename);
    BitmapDrawable bd = (BitmapDrawable) drawable;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        ShortcutManager shortcutManager =
                context.getSystemService(ShortcutManager.class);

        if (shortcutManager.isRequestPinShortcutSupported()) {
            ShortcutInfo pinShortcutInfo =
                    new ShortcutInfo.Builder(context, "app-shortcut")
                            .setIntent(intent)
                            .setShortLabel(shortcutname)
                            .setIcon(Icon.createWithAdaptiveBitmap(bd.getBitmap()))
                            .build();

            Intent pinnedShortcutCallbackIntent =
                    null;
            pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(pinShortcutInfo);

            PendingIntent successCallback = PendingIntent.getBroadcast(context, 0,
                    pinnedShortcutCallbackIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            shortcutManager.requestPinShortcut(pinShortcutInfo,
                    successCallback.getIntentSender());

        }
    }
}

但是我所有的图标都是用这个徽章创建的。

据我了解,您不能只删除它。但在 ume 图标转换器和其他应用程序中,这是通过某种方式完成的 through the widget

如何实现类似的功能?

【问题讨论】:

标签: java android android-widget android-shortcut


【解决方案1】:

是的,您可以通过已弃用的 Intent.EXTRA_SHORTCUT_INTENT 意图类型通过小部件创建快捷方式来创建没有该应用程序图标徽章的快捷方式。创建一个过滤<action android:name="android.intent.action.CREATE_SHORTCUT" /> 的活动,并通过手动创建Intent.EXTRA_SHORTCUT_INTENT 意图(而不是通过shortcutManager.createShortcutResultIntent())在该活动中创建快捷方式并使用setResult() 返回它。

详情请参阅this answer。请注意,使用不同的启动器时实际行为可能仍然不同(在某些启动器下可能不起作用)。我只在 Android 11 和 Android 12 下使用 Pixel Launcher 和 Microsoft Launcher 进行了测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    • 2018-07-04
    • 1970-01-01
    • 2012-03-22
    • 2013-03-10
    相关资源
    最近更新 更多