Forum Xamarin.Forms

Automatic answer when the phone rings?

PL.DeividPL.Deivid PLMember
edited May 2017 in Xamarin.Forms

Hi, this is my first post, and I have a problem with the code.
This is code:


[IntentFilter(new[] { "android.intent.action.PHONE_STATE" })]
public class IncomingPhoneCallDetector : BroadcastReceiver {
    public override void OnReceive(Context context, Intent intent) {
        if (intent.Extras != null) {
            string state = intent.GetStringExtra(TelephonyManager.ExtraState);
            string telephone = intent.GetStringExtra(TelephonyManager.ExtraIncomingNumber);
            if (state == TelephonyManager.ExtraStateRinging) {

                if (!string.IsNullOrEmpty(telephone)) {
                    Toast.MakeText(context, "Incoming call from " + telephone + ".", ToastLength.Short).Show();


                 Intent buttonDown = new Intent(Intent.ActionMediaButton);
               buttonDown.PutExtra(Intent.ExtraKeyEvent, new KeyEvent(KeyEventActions.Down, Keycode.Headsethook));
               context.SendOrderedBroadcast(buttonDown, "android.permission.CALL_PRIVILEGED");

                 Intent buttonUp = new Intent(Intent.ActionMediaButton);
                 buttonUp.PutExtra(Intent.ExtraKeyEvent, new KeyEvent(KeyEventActions.Up, Keycode.Headsethook));
                 context.SendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");



When the phone detects a call, the number is read by the application, but I do not know how to start the call answer action.
Can anyone help me with writing this action ?

I'm learning programming for 2 months, so please advice :)

I found on the internet how to reject the call. Only how to convert it to receiving a call ...

                 var manager = (TelephonyManager)context.GetSystemService(Context.TelephonyService);

                  IntPtr TelephonyManager_getITelephony = JNIEnv.GetMethodID(

                  IntPtr telephony = JNIEnv.CallObjectMethod(manager.Handle, TelephonyManager_getITelephony);
                  IntPtr ITelephony_class = JNIEnv.GetObjectClass(telephony);

                IntPtr ITelephony_endCall = JNIEnv.GetMethodID(
                JNIEnv.CallBooleanMethod(telephony, ITelephony_endCall);
Sign In or Register to comment.