接听电话时如何获取事件 [英] How to get event when i am answering outgoing call

查看:33
本文介绍了接听电话时如何获取事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能帮我了解如何检测拨出电话是否被接听(我需要从接听到掉线录音)?我可以检测到来电但不能检测到它.所以请帮忙.

Can you help me to understand how to detect whether the outgoing call is answered or not ( I need to record call starting from answer till dropping)? I can detect it for incoming calls but not for outgoing. So please help.

推荐答案

使用 TelephonyManager.ActionPhoneStateChanged 监视 TelephonyManager 状态,收到 TelephonyManager.ExtraStateIdle 你知道电话收音机现在什么时候空闲(没有通话正在进行).

Use TelephonyManager.ActionPhoneStateChanged to monitor the TelephonyManager state, upon receiving TelephonyManager.ExtraStateIdle you know when phone radio is now idling (no call in process).

[BroadcastReceiver(Name = "com.sushhangover.OutgoingCallBroadcastReceiver")]
[IntentFilter(new[] { Intent.ActionNewOutgoingCall, TelephonyManager.ActionPhoneStateChanged })]
public class OutgoingCallBroadcastReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        switch (intent.Action)
        {
            case Intent.ActionNewOutgoingCall:
                var outboundPhoneNumber = intent.GetStringExtra(Intent.ExtraPhoneNumber);
                Toast.MakeText(context, $"Started: Outgoing Call to {outboundPhoneNumber}", ToastLength.Long).Show();
                break;
            case TelephonyManager.ActionPhoneStateChanged:
                var state = intent.GetStringExtra(TelephonyManager.ExtraState);
                if (state == TelephonyManager.ExtraStateIdle)
                    Toast.MakeText(context, "Phone Idle (call ended)", ToastLength.Long).Show();
                else if (state == TelephonyManager.ExtraStateOffhook)
                    Toast.MakeText(context, "Phone Off Hook", ToastLength.Long).Show();
                else if (state == TelephonyManager.ExtraStateRinging)
                    Toast.MakeText(context, "Phone Ringing", ToastLength.Long).Show();
                else if (state == TelephonyManager.ExtraIncomingNumber)
                {
                    var incomingPhoneNumber = intent.GetStringExtra(TelephonyManager.ExtraIncomingNumber);
                    Toast.MakeText(context, $"Incoming Number: {incomingPhoneNumber}", ToastLength.Long).Show();
                }
                break;
            default:
                break;
        }
    }
}

注意:确保您向 ReadPhoneStateProcessOutgoingCalls 添加权限,以便此示例正常工作.

Note: Make sure you add permissions to ReadPhoneState and ProcessOutgoingCalls for this example to work.

这篇关于接听电话时如何获取事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆