简体   繁体   English

Xamarin如何以编程方式接听电话? 安卓系统

[英]How to answer a phone call programmatically Xamarin | Android

I'm building an app using xamarin android, I'm implementing a feature where I want to answer call programmatically; 我正在使用xamarin android构建应用程序,正在实现一项功能,我想以编程方式接听电话; I have an idea that I need to use Intent but how? 我有一个需要使用Intent的想法,但是如何使用? that's what I don't know. 那就是我不知道的。

Any one can suggest me? 有人可以建议我吗?

You can write a broadcast receiver that will manage your incoming call: 您可以编写一个广播接收器来管理您的来电:

[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();
                }
            }
        }
    }

Cheers!! 干杯!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM