Why my code is not launching the pinned shortcut of my App in Android Oreo or above?

I'm trying to pin some dynamic shortcuts of my app when the user creates a Custom time. I have two version of the code, but none of them is working as expected since one of them is trying to open the Play Store while the second one says that the App doesn't exist while I created the shortcut from the App in fact.

This one is launching the Play Store:

public void PinCustomTime(string time)
{
    var manager = context.GetSystemService(Context.ShortcutService) as ShortcutManager;

    if (manager.IsRequestPinShortcutSupported)
    {
        try
        {
            var intent = new Intent(Intent.ActionView);
            intent.AddFlags(ActivityFlags.NewTask);
            intent.SetData(Android.Net.Uri.Parse("market://details?id=" + context.PackageName));
            intent.PutExtra("customTime", time);
            var info = new ShortcutInfo.Builder(context, $"tmTimer_{DateTime.Now.ToString("yyMMddHHmmss")}")
                    .SetShortLabel("TM Timer")
                    .SetLongLabel("TM Timer")
                    .SetIcon(Icon.CreateWithResource(context, Resource.Drawable.iconInv))
                    .SetIntent(intent)
                    .Build();

            var successCallback = PendingIntent.GetBroadcast(context, /* request code */ 0,
            intent, /* flags */ 0);
            manager.RequestPinShortcut(info, successCallback.IntentSender);
        }
        catch (System.Exception ex)
        {

        }
    }
}

This one is saying that the app doesn't exist:

public void PinCustomTime(string time)
{
    var manager = context.GetSystemService(Context.ShortcutService) as ShortcutManager;

    if (manager.IsRequestPinShortcutSupported)
    {
        try
        {
            var info = new ShortcutInfo.Builder(context, $"tmTimer_{DateTime.Now.ToString("yyMMddHHmmss")}")
                    .SetShortLabel("TM Timer")
                    .SetLongLabel("TM Timer")
                    .SetIcon(Icon.CreateWithResource(context, Resource.Drawable.iconInv))
                    .SetIntent(new Intent(Intent.ActionView).SetData(Android.Net.Uri.Parse(context.PackageName)))
                    .Build();
            var intent = manager.CreateShortcutResultIntent(info);
            intent.PutExtra("customTime", time);

            var successCallback = PendingIntent.GetBroadcast(context, /* request code */ 0,
            intent, /* flags */ 0);
            manager.RequestPinShortcut(info, successCallback.IntentSender);
        }
        catch (System.Exception ex)
        {

        }
    }
}

I tried a third code, but that one was trying to open any app that wasn't my own app. Does anyone have experienced something similar? Or know what am I missing?

I have followed multiple tutorials and examples like these ones:

Thanks for your support.

Best Answer

  • FedericoNavarreteFedericoNavarrete SV ✭✭
    Accepted Answer

    I found a solution:

    public void PinCustomTime(string time)
    {
        var manager = context.GetSystemService(Context.ShortcutService) as ShortcutManager;
    
        if (manager.IsRequestPinShortcutSupported)
        {
            //Create the new intent
            var intent = new Intent(context, typeof(MainActivity));
            //Set the flag of the new task
            intent.SetAction(Intent.ActionView);
            //Set the Time
            intent.PutExtra("customTime", time);
            //Set the info of the shortcut
            var info = new ShortcutInfo.Builder(context, $"tmTimer_{DateTime.Now.ToString("yyMMddHHmmss")}")
                    .SetShortLabel("TM Timer")
                    .SetLongLabel("TM Timer")
                    .SetIcon(Icon.CreateWithResource(context, Resource.Drawable.iconInv))
                    .SetIntent(intent)
                    .Build();
    
            //Set values
            var successCallback = PendingIntent.GetBroadcast(context, /* request code */ 0,
            intent, /* flags */ 0);
            //Creates the shortcut
            manager.RequestPinShortcut(info, successCallback.IntentSender);
        }
    }
    

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    You can check the tutorial: https://www.jianshu.com/p/18be986553db

    A sample in native Android, hope it will help you. https://github.com/OptimusPrimeRen/Android-O-Adaptive

  • FedericoNavarreteFedericoNavarrete SVMember ✭✭
    Accepted Answer

    I found a solution:

    public void PinCustomTime(string time)
    {
        var manager = context.GetSystemService(Context.ShortcutService) as ShortcutManager;
    
        if (manager.IsRequestPinShortcutSupported)
        {
            //Create the new intent
            var intent = new Intent(context, typeof(MainActivity));
            //Set the flag of the new task
            intent.SetAction(Intent.ActionView);
            //Set the Time
            intent.PutExtra("customTime", time);
            //Set the info of the shortcut
            var info = new ShortcutInfo.Builder(context, $"tmTimer_{DateTime.Now.ToString("yyMMddHHmmss")}")
                    .SetShortLabel("TM Timer")
                    .SetLongLabel("TM Timer")
                    .SetIcon(Icon.CreateWithResource(context, Resource.Drawable.iconInv))
                    .SetIntent(intent)
                    .Build();
    
            //Set values
            var successCallback = PendingIntent.GetBroadcast(context, /* request code */ 0,
            intent, /* flags */ 0);
            //Creates the shortcut
            manager.RequestPinShortcut(info, successCallback.IntentSender);
        }
    }
    
Sign In or Register to comment.