Forum Xamarin.Android

Intent.ExtraShortcutIconResource is obsolete. What is the alternative?

khataulikhatauli Member ✭✭

I get the warning message that the above property is obsolete. But nowhere I found what do I replace it with.
Does anybody have any tips.

Thanks

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    This constant was deprecated in API level 26.

    Replaced with ShortcutManager.createShortcutResultIntent(ShortcutInfo).

    Refer https://developer.android.com/reference/android/content/Intent#EXTRA_SHORTCUT_ICON_RESOURCE .

  • ColeXColeX Member, Xamarin Team Xamurai

    Any Update @khatauli ?

  • khataulikhatauli Member ✭✭

    Appreciate the follow up @ColeX. To give a bit more context, I was following the link to add/display the shortcuts. If you look at time pointer 1:50 in the above video, where we use the coming Intent to parse the information about the shortcut, is the point where I got the obsolete warnings.
    Using your first reply, I tried to search online if I could somehow use ShortcutManager to achieve the same, but most of the examples I found online were only about how to create the shortcuts for your own app, but not for the scenario I am looking at.
    I am still lost as to how to translate what I have into using ShortcutManager.

  • ColeXColeX Member, Xamarin Team Xamurai

    but most of the examples I found online were only about how to create the shortcuts for your own app, but not for the scenario I am looking at.

    What shortcuts do you want ?

  • khataulikhatauli Member ✭✭

    My app is a launcher, where user may want to add shortcuts on the home screen from other apps. I have these methods, and the obsolete warnings are inside CreateShortcut() methed at the bottom.
    Here is the flow sequence:
    When user clicks a "add shortcut button", the first method is called, which displays the list of available shortcuts in the system. Once user selects the shortcut, the second method is called. After user does any needed configuration, and says ok, the CreateShortcut() method is called.

    Here is the code:
    public void AddShortcut()
    {
    var intent = new Intent(Intent.ActionPickActivity);
    var filter = new Intent(Intent.ActionCreateShortcut);
    intent.PutExtra(Intent.ExtraIntent, filter);
    this.mainActivity.StartActivityForResult(intent, Constants.REQUEST_PICK_SHORTCUT);
    }

        private void ConfigureShortcut(Intent data)
        {
            this.mainActivity.StartActivityForResult(data, Constants.REQUEST_CREATE_SHORTCUT);
        }
    
        private void CreateShortcut(Intent intent)
        {
            var iconResource = (Intent.ShortcutIconResource)(intent.GetParcelableExtra(Intent.ExtraShortcutIconResource));
            var bitmapIcon = (Bitmap)(intent.GetParcelableExtra(Intent.ExtraShortcutIcon));
            var shortcutLabel = intent.GetStringExtra(Intent.ExtraShortcutName);
            var shortIntent = (Intent)(intent.GetParcelableExtra(Intent.ExtraShortcutIntent));
            if (bitmapIcon == null)
            {
                if (iconResource != null)
                {
                    Resources resources = null;
                    try
                    {
                        resources = this.manager.GetResourcesForApplication(iconResource.PackageName);
                    }
                    catch { }
                    if (resources != null)
                    {
                        int id = resources.GetIdentifier(iconResource.ResourceName, null, null);
                        if (resources.GetDrawable(id) is StateListDrawable)
                        {
                            Drawable d = ((StateListDrawable)resources.GetDrawable(id)).Current;
                            bitmapIcon = ((BitmapDrawable)d).Bitmap;
                        }
                        else if (resources.GetDrawable(id) is BitmapDrawable)
                        {
                            bitmapIcon = ((BitmapDrawable)resources.GetDrawable(id)).Bitmap;
                        }
                        //are we missing some other clause?
                    }
                }
            }
    
            if (shortcutLabel != null && shortIntent != null && bitmapIcon != null)
            {
               //Display the shortcute
            }
            else
            {
                Utilities.ShowToast(
                    this.mainActivity, 
                    this.mainActivity.GetString(WinX.Resource.String.app_service_shortcut_launch_failed));
            }
        }
    
Sign In or Register to comment.