简体   繁体   English

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

[英]App creating more than one shortcut on home screen

My main activity has the following code 我的主要活动有以下代码

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

Every time I launch the app, it creates a new shortcut on the home screen. 每次启动该应用程序时,它都会在主屏幕上创建一个新的快捷方式。 Is there a standard or simple way to make sure only one home icon is created? 有没有一种标准或简单的方法来确保仅创建一个主页图标?

I am thinking of uninstalling the previous (if any) and then (re)installing. 我正在考虑先卸载以前的(如果有的话)然后再(重新)安装。 But that seems like an overkill. 但这似乎是一个过大的杀伤力。

shortcutintent.putExtra("duplicate", false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM