簡體   English   中英

Xamarin如何以編程方式接聽電話? 安卓系統

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

我正在使用xamarin android構建應用程序,正在實現一項功能,我想以編程方式接聽電話; 我有一個需要使用Intent的想法,但是如何使用? 那就是我不知道的。

有人可以建議我嗎?

您可以編寫一個廣播接收器來管理您的來電:

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

干杯!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM