0

I tried to deal with incoming calls to cancel or answer it but I can't. I tried the following code:

[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
                }
            }
        }
    }
}

I fetch the incoming phone number successfully but I can't answer or cancel it.So, I tried to use the action (ActionAnswer) below

Intent A = new Intent(Intent.ActionAnswer);

And as it's appearing in this screen shot from android developer site image the output and the input are nothing!!!. How can I deal with that action? or there are any other methods I can use to cancel the incoming calls? Thanks for advise. Here is the link of android developer. https://developer.android.com/reference/android/content/Intent.html#ACTION_ANSWER

Sabuncu
  • 5,095
  • 5
  • 55
  • 89
M Y
  • 464
  • 4
  • 7
  • 19
  • What are you trying to do? It should give people more context to go off of. – Jon Douglas Feb 01 '17 at 00:21
  • What are you trying to do? the action used to handle incoming call. Do you try to call the intent activity by code? – Mike Ma Feb 01 '17 at 01:44
  • Okay, I modified the question's body. – M Y Feb 01 '17 at 14:45
  • I think your code is working on android 4.0, but for android 5.0 the you can not do that follow the [answer](http://stackoverflow.com/questions/35438992/answer-incoming-call-in-android-6-0) here should be helpful for you. – Mike Ma Feb 02 '17 at 07:25
  • I am using android 4.4 and unfortunately, that isn't help me.That answer talk about android using Java and there is another link inside that link and also they talk about Java and I tried now to use them in Xamarin using C# but it failed and I got several errors in some functions. – M Y Feb 02 '17 at 18:23
  • no solution for that !? – M Y Feb 06 '17 at 21:52

1 Answers1

0

Finally, I found the solution and it works fine with me. That is the part we need.

// 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();
                    }
M Y
  • 464
  • 4
  • 7
  • 19