【问题标题】:Remove the app icon from the pinned shortcut android从固定的快捷方式android中删除应用程序图标
【发布时间】:2021-05-13 20:55:19
【问题描述】:

8 android之后,我的应用程序图标出现在快捷方式上。

我知道这是专门添加的,用于通知用户哪个应用创建了快捷方式。

这是当前用于创建图标的代码。

   ShortcutManager shortcutManager = (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
    BitmapDrawable bd = (BitmapDrawable) ic;
    if (shortcutManager.isRequestPinShortcutSupported()) {
        Intent shortcutInfoIntent = new Intent(context, MainActivity.class);
        shortcutInfoIntent.setAction(Intent.ACTION_VIEW);
        shortcutInfoIntent.putExtra("pckg", pck);

        ShortcutInfo info = new ShortcutInfo.Builder(context, "iconId")
                .setIcon(Icon.createWithBitmap(bd.getBitmap()))
                .setShortLabel(label)
                .setIntent(shortcutInfoIntent)
                .build();

        PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, MyReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);

        shortcutManager.requestPinShortcut(info, shortcutCallbackIntent.getIntentSender());
    }

我知道这是一个相当流行的问题,但没有人提供正常的解决方案。 但我需要以某种方式把它拿走

我确信这是可能的,因为它是在 x icon changer 中实现的,其中创建的图标没有这些徽章。

请写下你所有的想法如何解决它

【问题讨论】:

    标签: java android shortcut android-shortcut


    【解决方案1】:

    我确信这是可能的,因为它是在 x 图标转换器中实现的,其中创建的图标没有这些徽章。

    他们为此使用应用小部件。请参阅the Play Store listing that you linked to 中的“关于水印”。

    请写下你所有的想法如何解决它

    编写一个应用小部件。

    【讨论】:

    • 感谢您的回答,已经可以通过小部件移除徽章,我想找到一种方法在没有小部件的情况下将其移除
    • 同样在x icon changer中,不再需要创建widget,icon已经创建,没有badge,你可以自己试试
    • 嗨@Michael2322,您说已经可以通过小部件删除徽章,您能否详细说明如何通过小部件创建快捷方式来删除徽章?仅添加过滤CREATE_SHORTCUT 意图并在该活动中创建快捷方式的新活动似乎不起作用,不确定是否需要其他操作。谢谢。
    【解决方案2】:

    显然使用推荐的shortcutManager.createShortcutResultIntent() 会导致在它创建的快捷方式的一角显示应用程序图标,但不推荐使用的Intent.EXTRA_SHORTCUT_INTENT 方式仍然有效,如果您使用它,创建的快捷方式中不会有应用程序图标方式并通过小部件屏幕创建您的快捷方式!

    所以第一步是让您的快捷方式显示在小部件屏幕上。为此,请确保 <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/> 在您的 AndroidManifest.xml 文件中,并创建一个过滤 <action android:name="android.intent.action.CREATE_SHORTCUT"/> 的活动。清单文件将如下所示。

    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
    
        <application
            ...>
    
            ...
            <activity
                android:name=".shortcuts.YourShortcut"
                android:label="@string/shortcut_name"
                android:icon="@mipmap/ic_launcher"
                android:exported="true"
                ...>
                <intent-filter>
                    <action android:name="android.intent.action.CREATE_SHORTCUT" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
        ...
    

    在您的活动中,您需要创建一个快捷方式。通常我们使用setResult()RESULT_OK 作为结果代码和shortcutManager.createShortcutResultIntent() 为第二个参数创建意图,这将导致应用程序图标显示在快捷方式的角落。为了避免出现快捷方式图标,我们可以使用已弃用的Intent.EXTRA_SHORTCUT_INTENT 类型手动创建意图。比如:

        fun onOpen() {
            // things to do when user click the shortcut...
        }
    
        private fun setupShortcut() {
            val intent = Intent().putExtra(Intent.EXTRA_SHORTCUT_INTENT, Intent(this, this::class.java)) // we use this::class.java here, then clicking the shortcut will trigger onOpened()
                    .putExtra(Intent.EXTRA_SHORTCUT_NAME, shortLabel)
                    .putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, iconResource));
    
            setResult(
                RESULT_OK,
                intent
            )
        }
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            if (intent.action == Intent.ACTION_CREATE_SHORTCUT) {
                setupShortcut()
            } else {
                onOpened()
            }
            finish()
        }
    

    完成这些操作后,您可以看到快捷方式出现在小部件屏幕上。当用户将图标拖到启动器时,也不会在角落显示应用程序图标。

    请注意,使用不同的启动器时实际行为可能仍然不同(在某些启动器下可能无法正常工作)。上述代码在 Android 11 和 Android 12 下使用 Pixel Launcher 和 Microsoft Launcher 进行测试。

    【讨论】:

      猜你喜欢
      • 2019-04-12
      • 1970-01-01
      • 2015-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      相关资源
      最近更新 更多