卸载安卓应用时,应用启动器图标未从主屏幕中删除

7
我正在使用类似下面展示的代码片段来在主屏幕上添加应用程序快捷方式:
    Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
    shortcutIntent.setClassName(this, this.getClass().getName());
    shortcutIntent.putExtra(EXTRA_KEY, "ApiDemos Provided This Shortcut");

    // Then, set up the container intent (the response to the caller)

    Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcut_name));
    Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
            this,  R.drawable.app_sample_code);
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

    // Now, return the result to the launcher

    setResult(RESULT_OK, intent);

在创建快捷方式时没有任何问题,但是当卸载应用程序时,在主屏幕上的快捷方式仍然存在。当卸载其他应用程序时,它们似乎都会删除相应的主屏幕快捷方式。这就是我试图通过“通过代码创建的快捷方式图标”来实现的。
在Stackoverflow上,有没有Android专家知道卸载应用程序时如何从主屏幕中删除应用程序快捷方式?
我找到了一些相关的线程,但它们没有为我的问题提供解决方案,但请随意查看:
[0] https://developer.android.com/intl/de/resources/samples/ApiDemos/src/com/example/android/apis/app/LauncherShortcuts.html [1] Remove application from launcher programatically in Android [2] How to remove application shortcut from home screen on uninstall automatically?

1
卸载其他应用程序时,它们似乎也会删除其相应的主屏幕快捷方式。你能举出任何例子吗?据我所知,这甚至不可能。如果您有一个按照此方式工作的具体示例,请@我回来。 - CommonsWare
3
应用程序可以卸载它们的快捷方式,如果它们希望这样做的话,它们可以使用一个权限/意图来实现(它们也可以安装快捷方式)。然而,卸载应用程序应该自动清理快捷方式。这可能是您正在运行的Android / Launcher版本的错误。 - Romain Guy
1
@CommonsWare 举个例子:ASTRO文件管理器在应用内部有一个选项,可以将快捷方式添加到主屏幕。如果您选择这样做,然后卸载ASTRO应用程序。创建的主屏幕快捷方式也会被删除。(使用HTC Desire 2.1-u1) - Vidar Vestnes
2个回答

4

我认为你可以尝试将此操作放在第二个意图中:"com.android.launcher.action.INSTALL_SHORTCUT"

这对我有效,启动器图标会被安装到主屏幕上,当我卸载应用程序时,该图标也会被删除。我已经努力解决这个问题一段时间了。

Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(this, this.getClass().getName());

Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
            this,  R.drawable.launcher_icon);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
sendBroadcast(intent);

希望这能帮到你。

2

我也遇到了同样的问题。

最后,我发现在创建应用程序快捷方式时,应用程序的意图必须包含 Intent.ACTION_MAIN 操作,否则在卸载应用程序时,快捷方式将无法从主屏幕中删除(不是用于安装快捷方式的意图,该意图具有 com.android.launcher.action.INSTALL_SHORTCUT 操作)。

希望能对你有所帮助。


1
该应用程序一直使用“android.intent.action.MAIN”作为意图过滤器。所以,这并不是我的问题。但还是感谢。 - Vidar Vestnes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接