Xamarin.Forms Speakerphone Stop Activating Suddenly on Android 10

asked 2023-01-17T22:27:57.81+00:00
Universal Camera 286 Reputation points

I have App that has been working for years on Android 10, 11, 12 to Turn On/Off SpeakerPhone for Inbound and Outbound calls, but now it works sometimes on Inbound calls but never on Outbound calls. Here is a simple BroadcastReciever and MainActivity.

[BroadcastReceiver(Enabled = true, Exported = true)]

[IntentFilter(new[] { TelephonyManager.ActionPhoneStateChanged })]

public class PhonecallReceiver : BroadcastReceiver

{

public override async void OnReceive(Context context, Intent intent)

{

    Toast.MakeText(context, "Received intent!", ToastLength.Long).Show();



    var state = intent.GetStringExtra(TelephonyManager.ExtraState);

    

    if (intent.Action == TelephonyManager.ActionPhoneStateChanged)

    {

        if (state == TelephonyManager.ExtraStateRinging)

        {

            //Task.Run(async () =>

            //{

            //    await Task.Delay(1);

            //    Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>

            //    {

            //        MainActivity.audioManager = (AudioManager)Application.Context.GetSystemService(Context.AudioService);

            //        if (!MainActivity.audioManager.SpeakerphoneOn)

            //        { MainActivity.audioManager.SpeakerphoneOn = true; }

            //        var mode = Mode.Normal;// | Mode.Ringtone | Mode.InCommunication | Mode.Normal;

            //        MainActivity.audioManager.SetRouting(mode, Route.Speaker, Route.All);

            //    });

            //});

            MainActivity.audioManager.SpeakerphoneOn = true;

        }

        else if (state == TelephonyManager.ExtraStateOffhook)

        {

            MainActivity.audioManager.SpeakerphoneOn = true;

        }

        else if (state == TelephonyManager.ExtraStateIdle)

        {

            MainActivity.audioManager.SpeakerphoneOn = false; 

        }

    }

}

}

In the MainActivity

        public static AudioManager audioManager { get; set; }

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());

            var permissions = new string[]
            {
                Manifest.Permission.ReadPhoneState,
                Manifest.Permission.ModifyAudioSettings, 
                Manifest.Permission.ModifyPhoneState
            };
            ActivityCompat.RequestPermissions(this, permissions, 123);
           
            audioManager = (AudioManager)Application.Context.GetSystemService(Context.AudioService);
        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
          
            if (requestCode == 123 && grantResults.Length > 0 && grantResults[0] == Permission.Granted)
            {
                RegisterReceiver(new PhonecallReceiver(), new IntentFilter(TelephonyManager.ActionPhoneStateChanged));
            }
        }

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
4,804 questions
{count} votes