Reputation: 23
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
Reputation: 16459
Well in native android you have the telephony manager to check what is the state of your device:
It has three states:
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