Android:如何从主屏幕删除/卸载应用程序快捷方式?

标签 android

我一直在尝试(添加然后)从主屏幕删除我的应用程序的快捷方式。 添加快捷方式非常有效,但我无法删除使用以下代码创建的快捷方式。

public void setupShortCut(boolean create) {
        shortcutIntent = new Intent();
        shortcutIntent.setClassName("com.abc.xyz", "XYZActivity");
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);

        Intent intentShortcut = new Intent();
        intentShortcut.putExtra("android.intent.extra.shortcut.INTENT", shortcutIntent);
        intentShortcut.putExtra("android.intent.extra.shortcut.NAME", getResources().getString(R.string.app_name));
        intentShortcut.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", icon);
        if(create) {
          intentShortcut.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        } else {
        intentShortcut.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
        }
        sendBroadcast(intentShortcut);
    }

请指出我哪里出错了?

编辑 1:

我在我的 list 文件中获得了所需的权限:

<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /

最佳答案

给你:

private void deleteShortCut(Context context) {

    Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
    shortcutIntent.setClassName("com.example.androidapp", "SampleIntent");
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    shortcutIntent.putExtra("someParameter", "HelloWorld");

    Intent removeIntent = new Intent();
    removeIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    removeIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ShortcutName");
    removeIntent.putExtra("duplicate", false);

    removeIntent
            .setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");       
    context.sendBroadcast(removeIntent);
}

关于Android:如何从主屏幕删除/卸载应用程序快捷方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12853443/

相关文章:

java - 寻找一个Android XML教程

android - 未从子 Activity 调用 OnActivityResult

android - 如何检查除法的结果是int还是float

android - 我怎样才能 "meteor run android-device"到我的 meteor 服务器?

android - 在 Kotlin 中使用星投影作为父类(super class)型

android - 为什么我在 Android 应用程序中的自定义字体在 Release模式下被默认字体替换?

android - Android的 "Full Disc Encryption"和 "Storage Encryption"是一样的吗?

Android 的全屏尺寸

android - 如何访问回收站 View 中项目的查看器?

android - AAPT : Attribute "layout_anchorGravity" already defined with incompatible format. AND 25.0.3\aapt.exe'' 以非零退出值 1 完成