Irfan
Irfan

Reputation: 23

Xamarin Forms How to know when a call is currently taking place

I am writing some code for a Xamarin Forms Android app which dials a phone number but I don't want to dial the number if the user is currently on a call(whether incoming or outgoing, it doesn't make a difference). I have researched a lot about the phone state but I can't find what I am looking for, unless I am applying it incorrectly. What I need is something like this:

if (NoCurrentCallIsTakingPlace)
{
    var uri = Android.Net.Uri.Parse(string.Format("tel:{0}", PhoneNumber));
    var intent = new Intent(Intent.ActionCall, uri);
    Xamarin.Forms.Forms.Context.StartActivity(CurrentIntent);
}

The code to dial the number works but it's the the conditional statement/code to check the phone state that I am having a problem with. Please could someone help. Apologies if your need more info. Please let me know and I will provide it. Thank you.

Upvotes: 0

Views: 1342

Answers (1)

FreakyAli
FreakyAli

Reputation: 16459

Well in native android you have the telephony manager to check what is the state of your device:

It has three states:

  • Idle: when it's idle there is no call
  • Offhook: when Off-hook it is in call
  • Ringing: when Ringing

        var telephonyManagerService = (TelephonyManager)Xamarin.Forms.Forms.Context.GetSystemService(TelephonyService);
        var getCurrentState = telephonyManagerService?.CallState;
        switch (getCurrentState)
        {
            case CallState.Idle:
              //No call
                break;
            case CallState.Ringing:
              //Ringing 
                break;
            case CallState.Offhook:
              //On call
                break;
            default:
                break;
        }
    

Revert in case of queries

Upvotes: 1

Related Questions