[MAUI] How to create home shortcut for app on Android

Camille 26 Reputation points
2024-05-27T14:27:31.6933333+00:00

I've been searching, with no luck so far, how to add a shortcut for my MAUI .NET 8.0.5 application on Android 10 (API 29) home screen.

There are two Android permissions in the manifest

  • INSTALL_SHORTCUT
  • UNINSTALL_SHORTCUT

But neither seem to produce any result.

Nothing seems to be documented in any way on how to achieve this.

Any help is appreciated.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,231 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 72,251 Reputation points Microsoft Vendor
    2024-05-28T02:37:16.2066667+00:00

    Hello,

    But neither seem to produce any result.

    You added the permission is right, but you need to specific android platform code to add the short icon as well. Firstly, If I want to add short cut when I click the button event. You need to use Conditional compilation to wrap the android specific code.

    For Android, you can add short icon by AndroidX.Core.Content.PM.ShortcutManagerCompat

    You can refer to the following code.

    private void OnCounterClicked(object sender, EventArgs e)
            {
    #if ANDROID
                //on Home screen
                if (AndroidX.Core.Content.PM.ShortcutManagerCompat.IsRequestPinShortcutSupported(Platform.AppContext))
                {
                    AndroidX.Core.Content.PM.ShortcutInfoCompat shortcutInfo = new AndroidX.Core.Content.PM.ShortcutInfoCompat.Builder(Platform.AppContext, "#1")
                            .SetIntent(new Android.Content.Intent(Platform.AppContext, typeof(MainActivity)).SetAction(Android.Content.Intent.ActionMain)) // !!! intent's action must be set on oreo
                    .SetShortLabel("Test")
                    .SetIcon(AndroidX.Core.Graphics.Drawable.IconCompat.CreateWithResource(Platform.AppContext, Resource.Mipmap.appicon))
                    .Build();
                    AndroidX.Core.Content.PM.ShortcutManagerCompat.RequestPinShortcut(Platform.AppContext, shortcutInfo, null);
                }
                else
                {
                    // Shortcut is not supported by your launcher
                }
    #endif
            }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful