簡體   English   中英

Xamarin:如何處理 ACTION_ANSWER 活動

[英]Xamarin: How to deal with an ACTION_ANSWER Activity

我試圖處理來電以取消或接聽它,但我不能。 我嘗試了以下代碼:

[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 開發者網站圖像的屏幕截圖中時,輸出和輸入什么都不是!!!。 我該如何處理那個動作? 或者我可以使用其他任何方法來取消來電? 謝謝指教。 這是android開發人員的鏈接。 https://developer.android.com/reference/android/content/Intent.html#ACTION_ANSWER

最后,我找到了解決方案,它對我來說很好用。 那就是我們需要的部分。

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

暫無
暫無

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

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