Android M快捷方式安装/卸载重复

iGo*_*oDa 4 android android-intent android-6.0-marshmallow

我试图通过服务为我的应用程序创建一个快捷方式.以下代码正在处理SDK <= 21,但它无法正常工作SDK = 23

快捷方式的创建方式是这样完成的:

        Intent shortcutIntent = new Intent(this, MainActivity.class);
        shortcutIntent.putExtra(EXTRA_SHORTCUT_CLICKED, true); //This is only to check when the user clicked on the created shortcut
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

        Intent addIntent = new Intent();
        addIntent.putExtra("duplicate", false);
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "TEST");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.application_icon));
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);

        sendBroadcast(addIntent);
Run Code Online (Sandbox Code Playgroud)

SDK <= 21创建快捷方式中,如果已存在,则不会创建其他实例.

SDK = 23快捷方式将仅如果我按创建shorcut并再次尝试创建快捷方式.如果我重新启动该设备,然后再次尝试创建快捷方式复制或.

我尝试先卸载快捷方式,但没有成功SDK 23,如下所示:

        Intent shortcutIntent = new Intent(this, MainActivity.class);
        shortcutIntent.putExtra(EXTRA_SHORTCUT_CLICKED, true); //This is only to check when the user clicked on the created shortcut
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

        Intent addIntent = new Intent();
        addIntent.putExtra("duplicate", false);
        addIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "TEST");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.application_icon));
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);

        sendBroadcast(addIntent);
Run Code Online (Sandbox Code Playgroud)

这是服务实现:

public class MyService extends Service {

 @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        createShortcut(); //This is where I create the shortcut
        return super.onStartCommand(intent,flags,startId);
    }
}
Run Code Online (Sandbox Code Playgroud)

Android 6改变了捷径创作政治吗?我在文档上找不到任何内容.

[ 更新 ]我尝试通过单击示例应用程序上的按钮来创建此快捷方式,但这仍然会发生.单击创建的快捷方式后,shorcut将复制,然后再次尝试创建它.重启设备也会发生同样的事情.

[ UPDATE2 ]我正在查看Launcher3谷歌应用程序的grep代码,我发现快捷方式安装调用registerSessionCallback,这取决于调用线程.这些新变化可能会成为问题吗?

iGo*_*oDa 5

对于那些陷入同样问题的人:

谷歌删除了对Launcher3和谷歌上的卸载快捷方式的支持,现在是Android M上的Launcher.

此外,快捷方式的重复是最新的Android M版本(MRA58K)和现在开发团队手中的问题.

你们可以在这里关注这个问题,https://code.google.com/p/android/issues/detail?id = 179697

[ 更新 ]谷歌刚刚解决了这个问题,它将在下一个版本上发布.

  • 我也没有找到任何关于这方面的文档,但也是正常的,因为android API从未完全支持安装和卸载shorcuts.虽然PM amruthav说:"我们已经完全取消了对AOSP Launcher3和Google Now Launcher中UNINSTALL_SHORTCUT意图的支持.目前没有替代或替代此API." 我也测试了这个,并且在Android M上,这是不可能的,就像我在之前的Android版本上做的那样. (2认同)