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

时间:2015-10-09 11:09:44

标签: 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);

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

SDK = 23中,如果我按下创建的短信并尝试再次创建快捷方式,或者如果我重新启动设备然后再次尝试创建快捷方式,则只会复制快捷方式。

我尝试先卸载快捷方式但在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);

这是服务实现:

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

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

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

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

2 个答案:

答案 0 :(得分:5)

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

Google已取消对Launcher3和Google上的卸载快捷方式的支持,现在是Android M上的Launcher。

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

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

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

答案 1 :(得分:2)

也许这对你们中的一些人有帮助。谷歌删除了支持 用于卸载指向活动别名的快捷方式快捷方式 一旦别名被禁用,仍将被删除。 ;)