Java 安装快捷方式而不复制手动创建的本机快捷方式

Java 安装快捷方式而不复制手动创建的本机快捷方式,java,android,shortcut,Java,Android,Shortcut,我想为我的应用程序添加一个快捷方式,但我无法不复制本机手动创建的快捷方式(例如,通过使用应用程序菜单中的应用程序图标拖放到主屏幕) 这是我的密码: public void addShortcut(Context context) { this.manageShortcutAction(context, "com.android.launcher.action.INSTALL_SHORTCUT"); } public void deleteShortcut(Context context

我想为我的应用程序添加一个快捷方式,但我无法不复制本机手动创建的快捷方式(例如,通过使用应用程序菜单中的应用程序图标拖放到主屏幕)

这是我的密码:

public void addShortcut(Context context)
{
    this.manageShortcutAction(context, "com.android.launcher.action.INSTALL_SHORTCUT");
}

public void deleteShortcut(Context context)
{
    this.manageShortcutAction(context, "com.android.launcher.action.UNINSTALL_SHORTCUT");
}

private void manageShortcutAction(Context context, String intentAction)
{
    Context applicationContext = context.getApplicationContext();
    Intent shortcut = new Intent(intentAction);
    ApplicationInfo appInfo = applicationContext.getApplicationInfo();
    PackageManager packageManager= applicationContext.getPackageManager();
    String applicationName = (String) packageManager.getApplicationLabel(appInfo);

    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, applicationName); // Shortcut name
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, packageManager.getLaunchIntentForPackage(appInfo.packageName));// Setup activity should be shortcut object 
    shortcut.putExtra("duplicate", false); // Just create once
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(applicationContext, appInfo.icon));// Set shortcut icon

    applicationContext.sendBroadcast(shortcut);
}

以及我的清单所需的权限:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />


顺便说一下,我已经覆盖了应用程序代码,现在是
MainApplication
extensing
Application
。 我已经尝试创建一个组件来创建
Intent.EXTRA\u SHORTCUT\u Intent
,但没有预期的结果


如果有人有想法…

在与索尼的开发者讨论后,没有办法不复制手动创建的快捷方式


我为三星找到了一些方法,但这些都不是通用的。

在与索尼的开发者讨论后,没有办法不复制手动创建的快捷方式


我为三星找到了一些方法,但这些方法不是通用的。

我发现,为了防止应用程序创建应用程序安装时创建的快捷方式的副本(或通过从菜单复制),我必须使用与现有快捷方式相同的参数创建一个快捷方式-使用相同的快捷方式名称是不够的

经过大量测试并感谢logcat,我已经能够创建一个精确的副本,如下所示:

private void installShortcut() {
    final Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
    shortcutIntent.setAction(Intent.ACTION_MAIN);
    shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

    final Intent intent = new Intent();
    intent.putExtra("duplicate", false);
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    sendBroadcast(intent);
}
与我在SO上找到的其他答案的区别在于以下三行:

    shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
通过查看使用系统创建的快捷方式手动启动应用程序时打印的日志条目,我发现了这一点

Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.myapp/.activities.main.MainActivity bnds=[365,73][475,191] } from pid 279

我发现,为了防止应用程序创建应用程序安装时创建的快捷方式的副本(或通过从菜单复制),我必须使用与现有快捷方式相同的参数创建一个快捷方式-使用相同的快捷方式名称是不够的

经过大量测试并感谢logcat,我已经能够创建一个精确的副本,如下所示:

private void installShortcut() {
    final Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
    shortcutIntent.setAction(Intent.ACTION_MAIN);
    shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

    final Intent intent = new Intent();
    intent.putExtra("duplicate", false);
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    sendBroadcast(intent);
}
与我在SO上找到的其他答案的区别在于以下三行:

    shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
通过查看使用系统创建的快捷方式手动启动应用程序时打印的日志条目,我发现了这一点

Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.myapp/.activities.main.MainActivity bnds=[365,73][475,191] } from pid 279

如果有人有一天想这么做,我会有兴趣把这个答案标记为最好的答案。目前,我没有时间进行测试(我的市场部要求我删除此功能)。感谢您的回答。根据最后的评论(不是我自己测试),我将已解决的响应更改为此。我希望它涵盖所有制造商,而不是棉花糖。在S5和GAPPS模拟器上测试。我真的认为这与你使用的是什么样的启动程序有关,但我可能错了。它不适用于谷歌QuickSearchBox启动器。它与AOSP launcher1(一种用于im测试的marshmello平板电脑)配合使用。然而,一个名为“TopBuzz”的应用程序似乎已经完美地实现了它,但我不知道他们是如何做到的!如果有人有一天想这么做,我会有兴趣把这个答案标记为最好的答案。目前,我没有时间进行测试(我的市场部要求我删除此功能)。感谢您的回答。根据最后的评论(不是我自己测试),我将已解决的响应更改为此。我希望它涵盖所有制造商,而不是棉花糖。在S5和GAPPS模拟器上测试。我真的认为这与你使用的是什么样的启动程序有关,但我可能错了。它不适用于谷歌QuickSearchBox启动器。它与AOSP launcher1(一种用于im测试的marshmello平板电脑)配合使用。然而,一个名为“TopBuzz”的应用程序似乎已经完美地实现了它,但我不知道他们是如何做到的!快捷方式。putExtra(“重复”,false);在sdk 26中不工作,将创建over和overshortcut.putExtra(“复制”,false);在sdk 26中不工作,它将在sdk版本26和Android 6.0中反复创建,putExtra(“复制”,false),不工作,add all,它将反复创建!!家庭应用程序/启动器不必管理快捷方式(即使很糟糕)。在华为,它只是Apple Launcher的一个副本,其中只有一个应用程序列表和一些目录。SdkVersion 26和Android 6.0中的putExtra(“复制”,false),不工作,添加所有内容,它将反复创建!!家庭应用程序/启动器不必管理快捷方式(即使很糟糕)。在华为,它只是Apple Launcher的一个副本,其中只有一个应用程序列表和一些目录。