【问题标题】:Android: How to Remove/Uninstall application shortcuts from Home Screen?Android:如何从主屏幕删除/卸载应用程序快捷方式?
【发布时间】:2015-07-23 05:56:16
【问题描述】:

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

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:

我的 Manifest 文件已获得所需权限:

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

【问题讨论】:

  • 您在清单中有"com.android.launcher.permission.UNINSTALL_SHORTCUT" 权限吗?
  • 是的,我的清单中确实有这个权限。
  • 您是否绝对确定当您尝试删除您正在调用setupShortCut(false) 的快捷方式时?
  • 我对此非常肯定。我已经调试了代码,可以看到控件进入else

标签: android


【解决方案1】:

给你:

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);
}

【讨论】:

  • Devangi,感谢您的回复,但是您的代码与我发布的代码非常相似,只有一个例外,即:removeIntent.putExtra("duplicate", false); 我已将此代码添加到我的意图中,但它仍然无法正常工作.
  • 这是一个第 3 方启动器,它限制我卸载我创建的应用程序快捷方式。我已经编辑了我的问题。
  • 感谢您的代码,但此代码不适用于 api 级别 21 以上。所以您找到任何解决方案了吗?
【解决方案2】:

要删除快捷方式,请尝试以下代码...

final Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setComponent(new ComponentName(this.getPackageName(), "YourClassName"));

final Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));          
intent.setComponent(new ComponentName(this.getPackageName(), "YourClassName"));                     
intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");

sendBroadcast(intent, null);

在您的清单文件中添加以下权限:

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

【讨论】:

  • 感谢Priyank的回复,但是还是不行。它对你有用吗?
  • 仍然无法让它工作。 SHORTCUTS 有这个东西 - 为了使用意图删除它们,您必须以与创建它们时完全相同的方式设置意图的属性。
  • 这是一个第 3 方启动器,它限制我卸载我创建的应用程序快捷方式。我已经编辑了我的问题。
  • 是否可以删除应用程序的所有快捷方式,而不告诉 setComponent 每个快捷方式的类名?另外,为什么需要添加名称?真的需要吗?
【解决方案3】:

找出根本原因:-)

我找到了它对我不起作用的原因。 我在手机上使用了不同的第 3 方启动器(Stock android 启动器除外)。 只要您使用的启动器支持该操作,就可以创建和删除App-Shortcut。 我在默认启动器上运行了上面的代码,它就像一个魅力:)

谢谢大家的回复!

【讨论】:

  • 哪个启动器有问题?
  • 我明白了。它仍然可以与股票启动器一起正常工作吗?即使在较新的 Android 版本(Lollipop、Marshmallow、N)上?
  • @android 开发者:遗憾的是,我无法访问应用程序(具有此功能),也无法访问 Lollipop、Marshmallow 或 N 的代码。两年多前我曾尝试这样做.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多