如何以编程方式接听电话Xamarin | Android的

时间:2015-10-23 11:14:43

标签: android xamarin

我正在使用xamarin android构建应用程序,我正在实现一个功能,我想以编程方式回答呼叫;我有一个想法,我需要使用Intent,但如何?这就是我不知道的。

任何人都可以建议我吗?

1 个答案:

答案 0 :(得分:6)

您可以编写一个广播接收器来管理您的来电:

[BroadcastReceiver]
    [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);
                if (state == TelephonyManager.ExtraStateRinging)
                {
                    string telephone = intent.GetStringExtra(TelephonyManager.ExtraIncomingNumber);             
                    if (!string.IsNullOrEmpty (telephone)) 
                    {
                        Toast.MakeText (context, "Incoming call from " + telephone + ".", ToastLength.Short).Show ();
                    }
                    else 
                    {
                        Toast.MakeText (context, "Incoming call from unknown number.", ToastLength.Short).Show ();
                    }
                    Intent buttonDown = new Intent(Intent.ActionMediaButton);
                    buttonDown.PutExtra(Intent.ExtraKeyEvent, new KeyEvent(KeyEventActions.Up, Keycode.Headsethook));
                    context.SendOrderedBroadcast (buttonDown, "android.permission.CALL_PRIVILEGED");
                }
                else if (state == TelephonyManager.ExtraStateOffhook)
                {
                    Toast.MakeText(context, "Incoming call answered.", ToastLength.Short).Show();
                }
                else if (state == TelephonyManager.ExtraStateIdle)
                {
                    Toast.MakeText(context, "Incoming call ended.", ToastLength.Short).Show();
                }
            }
        }
    }

干杯!!