Monday, 10 June 2019

Why my code is not launching the pinned shortcut of my App in Android 7.1+?

I'm trying to pin some dynamic shortcuts of my app, they are going to be created 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 and the second one says: "the App doesn't exist" when I created the shortcut from the App.

This one is launching the Play Store:

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

    if (manager.IsRequestPinShortcutSupported)
    {
        try
        {
            //Create the new intent
            var intent = new Intent(Intent.ActionView);
            //Set the flag of the new task
            intent.AddFlags(ActivityFlags.NewTask);
            //Get the apps from the Play Store
            intent.SetData(Android.Net.Uri.Parse("market://details?id=" + context.PackageName));
            //Set the custom time as a variable
            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);
        }
        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
        {
            //Set the info of the shortcut with the App to open
            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();

            //Create the new intent
            var intent = manager.CreateShortcutResultIntent(info);
            intent.PutExtra("customTime", time);

            //Set values
            var successCallback = PendingIntent.GetBroadcast(context, /* request code */ 0,
            intent, /* flags */ 0);
            //Creates the shortcut
            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.

P.S.:

I have built the code in Xamarin.Android in C#, but if you have an idea in Kotlin or Java, I can migrate it.



from Why my code is not launching the pinned shortcut of my App in Android 7.1+?

No comments:

Post a Comment