Xamarin:如何处理ACTION_ANSWER活动

时间:2017-01-31 23:28:54

标签: android android-intent xamarin

我试图处理来电取消或接听,但我不能。 我用了那段代码

[BroadcastReceiver(Label = "Blocking Calls")]
[IntentFilter(new string[] { "android.intent.action.PHONE_STATE" })]
public class MyReceiver : Android.Content.BroadcastReceiver
{
    private const string IntentAction_BlockingCalls = "android.intent.action.PHONE_STATE";

    public override void OnReceive(Context context, Intent intent)
    {
        if (intent.Action == IntentAction_BlockingCalls)
        {
            // ensure there is information
            if (intent.Extras != null)
            {
                // get the incoming call state
                string state = intent.GetStringExtra(TelephonyManager.ExtraState);

                // check the current state
                if (state == TelephonyManager.ExtraStateRinging)
                {
                    // read the incoming call telephone number
                    string telephoneNumber = intent.GetStringExtra(TelephonyManager.ExtraIncomingNumber);

                    Intent buttonDown = new Intent(Intent.ActionMediaButton);
                    buttonDown.PutExtra(Intent.ActionView, new KeyEvent(KeyEventActions.Down, Keycode.Headsethook));
                    context.SendBroadcast(buttonDown);

                    Toast.MakeText(context, telephoneNumber, ToastLength.Short).Show();  // Flag 4        
                    // check the read telephone
                    if (string.IsNullOrEmpty(telephoneNumber))
                        telephoneNumber = string.Empty;
                }
                else if (state == TelephonyManager.ExtraStateOffhook)
                {
                    // Toast.MakeText(context, "The call is answered", ToastLength.Short).Show();  // Flag 5
                    // incoming call answer
                }
                else if (state == TelephonyManager.ExtraStateIdle)
                {
                    // Toast.MakeText(context, "The call have ended", ToastLength.Short).Show();  // Flag 6
                    // incoming call end
                }
            }
        }
    }
}

我成功获取传入的电话号码,但我无法接听或取消它。所以,我尝试使用下面的操作(ActionAnswer)

Intent A = new Intent(Intent.ActionAnswer);

因为它出现在Android开发者网站的这个屏幕上 image 输出和输入都没有!!! 我该如何处理这个行动?或者还有其他方法可以用来取消来电? 谢谢你的建议。 这是android开发者的链接。 https://developer.android.com/reference/android/content/Intent.html#ACTION_ANSWER

1 个答案:

答案 0 :(得分:0)

最后,我找到了解决方案,它对我很好。 这是我们需要的部分。

// check the current state
                if (state == TelephonyManager.ExtraStateRinging)
                {
                    // read the incoming call telephone number
                    string telephoneNumber = intent.GetStringExtra(TelephonyManager.ExtraIncomingNumber);

                    // You can use Endswith, Equals, or any other available methods
                    //if (telephoneNumber.EndsWith("604"))
                    if (telephoneNumber.Equals("01282790604"))
                    {
                         var manager = (TelephonyManager)context.GetSystemService(Context.TelephonyService);

                        IntPtr TelephonyManager_getITelephony = JNIEnv.GetMethodID(
                                manager.Class.Handle,
                                "getITelephony",
                                "()Lcom/android/internal/telephony/ITelephony;");

                        IntPtr telephony = JNIEnv.CallObjectMethod(manager.Handle, TelephonyManager_getITelephony);
                        IntPtr ITelephony_class = JNIEnv.GetObjectClass(telephony);
                        IntPtr ITelephony_endCall = JNIEnv.GetMethodID(
                                ITelephony_class,
                                "endCall",
                                "()Z");
                        JNIEnv.CallBooleanMethod(telephony, ITelephony_endCall);
                        JNIEnv.DeleteLocalRef(telephony);
                        JNIEnv.DeleteLocalRef(ITelephony_class);


                        Toast.MakeText(context, telephoneNumber + "Is Blocked", ToastLength.Long).Show();
                    }