【问题标题】:How can I pevent creating shortcut more than one time?如何防止多次创建快捷方式?
【发布时间】:2013-04-23 06:49:33
【问题描述】:

我编写了以下代码,用于在主屏幕中创建应用程序的快捷方式:

private void createShortcut() {

        Intent shortcutIntent = new Intent(this, ActActivation.class);
        shortcutIntent.setClassName("org.mabna.order",
                "org.mabna.order.ui.ActActivation");
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "test");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        this.sendBroadcast(addIntent);

    }

它可以正常工作,但是每次我运行我的程序并运行此代码时,我都会收到“快捷方式测试已创建”消息,并且我的主屏幕上会添加一个新快捷方式。打开我的应用程序 10 次后,我有 10 个快捷方式。

如何防止出现此消息框并创建多个快捷方式?

【问题讨论】:

  • SharePreferences 中设置boolean 并在每次运行时检查。在第一次运行时切换到true。我能想到的最简单的事情。
  • @IceMAN 如果用户清除应用程序数据,则重新创建快捷方式
  • @breceivemail 看看我的回答,我遇到了示例问题,但我已经解决了这个问题。

标签: android shortcut homescreen


【解决方案1】:

创建SharedPrefernce并添加此行,卸载不需要任何其他权限

addIntent.putExtra("duplicate", false);

更改后的代码

    Intent shortcutIntent = new Intent(this, ActActivation.class);
    shortcutIntent.setClassName("org.mabna.order",
            "org.mabna.order.ui.ActActivation");
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "test");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
    addIntent.putExtra("duplicate", false); // Just create once
    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    this.sendBroadcast(addIntent);

这将只在 HomeScreen 上创建一个图标,如果用户清除 Cache 这将阻止应用程序在 Homescreen 上创建另一个图标。

编辑:代码在 android 4.2(JellyBean) 中进行了测试,运行良好。

【讨论】:

  • addIntent.putExtra("duplicate", false); 不适用于 API 14 以上的设备。
  • 谢谢!很好的解决方案。如何阻止该消息?
  • @Rstar 我已经测试过了,没问题。但我注意到它不适用于一些新的 API。
【解决方案2】:

很遗憾,没有 API 有助于防止重复。也没有用于查看主屏幕上的内容的 API(至少在没有破解启动器数据的情况下是这样)。您可以做的最好的事情是跟踪您是否已经创建了快捷方式。共享偏好最容易做到这一点。请参阅storage options guide

反正还是走这条路比较好。如果您总是在当前不存在的情况下创建快捷方式,您将很快疏远任何手动删除您的快捷方式的用户。

【讨论】:

    【解决方案3】:

    最好的方法是使用SharedPreferences:

    private static final String SHORTCUT = "SHORTCUT";
    //...
    if (!preferences.readBool(SHORTCUT, false)) {
        Intent shortcutIntent = new Intent(getApplicationContext(),
                        SomeActivity.class);
    
        shortcutIntent.setAction(Intent.ACTION_MAIN);
    
        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                        Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                                R.drawable.app_icon));
        addIntent.putExtra("duplicate", false);
    
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(addIntent);
    
        SharedPreferences.Editor edit = preferences.edit();
        edit.putBoolean(SHORTCUT, true);
        edit.apply();
    }
    

    但是,如果用户清除应用数据并重新启动应用,将创建副本

    【讨论】:

    • 如果你删除缓存,你会得到重复的:)
    • 和其他应用程序。他们是怎么做到的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 2021-11-23
    • 2012-03-30
    • 1970-01-01
    • 2013-03-14
    • 1970-01-01
    相关资源
    最近更新 更多