首页 > 解决方案 > 在 Android 9.0 中创建进入主屏幕的快捷方式

问题描述

我使用以下代码创建快捷方式

private void createShortCut() {
        Intent shortcutIntent = new Intent(getApplicationContext(), SplashActivity.class);

        shortcutIntent.setAction(Intent.ACTION_MAIN);

        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getString(R.string.app_name));
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.mipmap.ic_launcher));
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        addIntent.putExtra("duplicate", false); 
        getApplicationContext().sendBroadcast(addIntent);
    }

上面的代码在 android 7.0 中运行良好,但在 android 8.0 及更高版本中不起作用。如何在 android 8.0 及更高版本的主屏幕中添加快捷方式?

标签: androidshortcuthomescreenandroid-9.0-pie

解决方案


请参考这篇文章: https
://developer.android.com/guide/topics/ui/shortcuts/creating-shortcuts你应该使用ShortcutManager这个目的。


推荐阅读