Android create shortcuts on the home screen

前端 未结 10 752
忘掉有多难
忘掉有多难 2020-11-22 13:16

What I want to do is:

1) I\'m inside an activity, there are 2 buttons. If I click the first one a shortcut is created in my home screen. The shortcut open an

10条回答
  •  花落未央
    2020-11-22 13:47

    The com.android.launcher.action.INSTALL_SHORTCUT broadcast no longer has any effect since android oreo. LINK

    If you want to support all android versions, especially android 8.0 or oreo and newer, use the code below to create shortcut:

    public static void addShortcutToHomeScreen(Context context)
    {
        if (ShortcutManagerCompat.isRequestPinShortcutSupported(context))
        {
            ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
                    .setIntent(new Intent(context, YourActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
                    .setShortLabel("Test")
                    .setIcon(IconCompat.createWithResource(context, R.drawable.ic_launcher))
                    .build();
            ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
        }
        else
        {
            // Shortcut is not supported by your launcher
        }
    }
    

    Add permission in manifest file:

    
    

提交回复
热议问题