Java 在主屏幕上创建多个快捷方式的应用程序

Java 在主屏幕上创建多个快捷方式的应用程序,java,android,shortcut,launcher,homescreen,Java,Android,Shortcut,Launcher,Homescreen,我的主要活动有以下代码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); … homescreenShortcut(); } private void homescreenShortcut() { Intent shortcutIntent = new Intent(); shortcutIntent.

我的主要活动有以下代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    …


    homescreenShortcut();
}

private void homescreenShortcut() {
    Intent shortcutIntent = new Intent();
    shortcutIntent.setClassName(this, this.getClass().getName());
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    shortcutIntent.addCategory(Intent.ACTION_PICK_ACTIVITY);
    Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MyApp");
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
        Intent.ShortcutIconResource.fromContext(this, R.drawable.app_icon));

    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    sendBroadcast(intent);
}
每次我启动应用程序时,它都会在主屏幕上创建一个新的快捷方式。是否有标准或简单的方法确保只创建一个主图标

我正在考虑卸载以前的(如果有),然后(重新)安装。但这似乎有点过分了

添加

shortcutintent.putExtra("duplicate", false);

不要每次从创建时调用homescreenShortcut()。创建一个布尔标志,并将其存储在shared Pref或表示您已创建1快捷方式的某个位置,然后将对
homescreenShortcut()
的调用包装在检查该标志的if语句中。除此之外,你真的不应该像这样自动创建快捷方式。如果用户想要一个快捷方式,他们会做一个,我认为你做这个自动快捷方式的事情可能会惹恼更多的人,而不是帮助更多的人。我同意,你不应该为用户这样做。让用户自己做。很抱歉,如果这种情况发生在我身上,我很可能会删除该应用程序。谢谢。但实际上我没有制定规则。我正在为别人做一个项目。使用addIntent.putExtra(“复制”,false)@juytuyutyuyt当你说
addint
时,你是指我的代码片段中的
shortcutient
还是
intent