I would like to receive call details such as number, name (if saved in contacts), dialed/received number, call duration, received/dialed date and time after every incoming and outgoing call.
I have created a phone call watcher class subscribed to base class BroadcastReceiver
like below:
using Android.App;
using Android.Content;
using Android.Telephony;
[BroadcastReceiver()]
[IntentFilter(new[] { "android.intent.action.PHONE_STATE", "android.intent.action.NEW_OUTGOING_CALL" }, Priority = (int)IntentFilterPriority.HighPriority)]
public class PhoneCallWatcher : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
// ensure there is information
if (intent.Extras != null)
{
if (intent.Action.Equals(Intent.ActionNewOutgoingCall))
{
//outgoing call
}
// get the incoming call state
string state = intent.GetStringExtra(TelephonyManager.ExtraState);
// check the current state
if (state == TelephonyManager.ExtraStateRinging)
{
// read the incoming call telephone number...
string telephone = intent.GetStringExtra(TelephonyManager.ExtraIncomingNumber);
// check the reade telephone
if (string.IsNullOrEmpty(telephone))
telephone = string.Empty;
}
else if (state == TelephonyManager.ExtraStateOffhook)
{
// incoming call answer
}
else if (state == TelephonyManager.ExtraStateIdle)
{
// incoming call end
}
}
}
}
and registering above service in MainActivity.cs
like below:
//starting phone call watcher service
var phoneCallWatcherIntent = new Intent(ApplicationContext, typeof(PhoneCallWatcher));
SendBroadcast(phoneCallWatcherIntent);
But the above code is not working when I make an outgoing call and receive an incoming call. Please help!