androidandroid-intentxamarinxamarin.formsringtonemanager

Xamarin.Forms alternative to StartActivityForResult


I am currently trying to get the Uri selected from a RingtoneManager.ActionRingtonePicker. I put in all my extra settings and set the appropriate flag. However I am only able to call StartActivity(intent). Is there anyway for me to get the selection from the Ringtone Picker and use the intent.getParceableExtra(RingtoneManager.ExtraRingtonePickedUri);. My code is below and if I can replace the context.StartActivity(intent) with a work around then that would be great.

public async Task<string> pickAndReceiveRingtone(string currentUri)
{
    Intent intent = new Intent(RingtoneManager.ActionRingtonePicker);
    intent.PutExtra(RingtoneManager.ExtraRingtoneShowSilent, false);
    intent.PutExtra(RingtoneManager.ExtraRingtoneTitle, "Select a ringtone");
    intent.PutExtra(RingtoneManager.ExtraRingtoneShowDefault, false);
    intent.PutExtra(RingtoneManager.ExtraRingtoneType, (int)RingtoneType.Alarm);
    intent.PutExtra(RingtoneManager.ExtraRingtoneExistingUri, RingtoneManager.GetDefaultUri(RingtoneType.Alarm));
    intent.SetFlags(ActivityFlags.NewTask);

    //Replace below
    /*await*/ context.StartActivity(intent);

    //Grab selected uri here
    currentUri = ...
    return  currentUri;

}

Solution

  • Actually, Xamarin.Forms exists in an Activity, OnActivityResult method in Activity still can be called.

    In your DependencyService class, use the StartActivityForResult() method like this :

    public void StartActivityInAndroid()
    {
        Intent intent = new Intent(RingtoneManager.ActionRingtonePicker);
        intent.PutExtra(RingtoneManager.ExtraRingtoneShowSilent, false);
        intent.PutExtra(RingtoneManager.ExtraRingtoneTitle, "Select a ringtone");
        intent.PutExtra(RingtoneManager.ExtraRingtoneShowDefault, false);
        intent.PutExtra(RingtoneManager.ExtraRingtoneType, (int)RingtoneType.Alarm);
        intent.PutExtra(RingtoneManager.ExtraRingtoneExistingUri, RingtoneManager.GetDefaultUri(RingtoneType.Alarm));
    
        //intent.SetFlags(ActivityFlags.NewTask);// remember to delete this
    
        var activity = Forms.Context as Activity;
    
        activity.StartActivityForResult(intent, 0);
    }
    

    Then, you can receive the result in OnActivityResult method like this :

        protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
            if (resultCode != Result.Ok)
            {
                return;
            }
            else
            {
                Android.Net.Uri uri = (Android.Net.Uri)data.GetParcelableExtra(RingtoneManager.ExtraRingtonePickedUri);
                Log.Debug("onActivityResult====", "" + uri);
    
                Toast.MakeText(this, uri + "", ToastLength.Short).Show();
                if (uri != null)
                {
                    switch (requestCode)
                    {
                        case 0:
                            RingtoneManager.SetActualDefaultRingtoneUri(this, RingtoneType.Ringtone, uri);
                            break;
                    }
                }
            }
        }