代码之家  ›  专栏  ›  技术社区  ›  Vidar Vestnes

卸载Android应用程序时,应用程序启动程序图标不会从主屏幕上删除。

  •  7
  • Vidar Vestnes  · 技术社区  · 15 年前

    我正在使用类似的代码片段,如下所示在主屏幕上添加应用程序快捷方式:

        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?

    2 回复  |  直到 9 年前
        1
  •  4
  •   Eric Nordvik    15 年前

    我想你可以试着把这个动作放到第二个意图中:“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
  •  2
  •   user267545    15 年前

    我也有同样的问题。

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

    希望它有帮助。

    推荐文章