title: Ending an outgoing call while it is offhook
body: <p>I need to end the call after the ring has been made (kind of like a missed call). I used <code>BroadcastReceiver</code> and <code>PhoneStateListener</code> to monitor the states.</p>
  
  <p>The problem is I'm receiving a broadcast and also the <code>phonenumber</code>, and the listener works well - it goes inside the <code>switch</code> and executes the <code>case</code> intended, but it doesn't execute the <code>endCall</code> method of the <code>ITelephony</code> interface. Here's my code:</p>
  
  <p><strong>Manifest:</strong></p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.CALL_PHONE" /&gt;
  &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.MODIFY_PHONE_STATE"
      tools:ignore="ProtectedPermissions" /&gt;
  &lt;uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/&gt;
  &lt;uses-permission android:name="android.permission.READ_CALL_LOG" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_CALL_LOG" /&gt;
  &lt;receiver
      android:name=".EndCall"
      android:enabled="true" &gt;
      &lt;intent-filter&gt;
          &lt;action android:name="android.intent.action.NEW_OUTGOING_CALL" /&gt;
      &lt;/intent-filter&gt;
  &lt;/receiver&gt;
  </code></pre>
  
  <p><strong>EndCall.java</strong></p>
  
  <pre><code>public class EndCall extends BroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
          Log.i("Debug", "Received a broadcast...");
          String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
          Log.i("Debug", "Phone number: " + phonenumber);
          TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
          EndCallListener callListener = new EndCallListener(context, tm);
          tm.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
      }
  }
  </code></pre>
  
  <p><strong>EndCallListener.java:</strong></p>
  
  <pre><code>public class EndCallListener extends PhoneStateListener {
      Context context;
      TelephonyManager tm;
      public EndCallListener(Context context, TelephonyManager tm) {
          this.context = context;
          this.tm = tm;
      }
      @Override
      public void onCallStateChanged(int state, String incomingNumber) {
          switch (state) {
              case TelephonyManager.CALL_STATE_OFFHOOK:
                  Log.i("Debug", "OFFHOOK");
                  try {
                      Log.i("Debug", "Inside try...");
                      Class c = Class.forName(tm.getClass().getName());
                      Method m = c.getDeclaredMethod("getITelephony");
                      ITelephony telephonyService = (ITelephony) m.invoke(tm);
                      //nothing happens after this...
                      if(telephonyService.endCall()) {
                          Log.i("Debug", "Call ended...");
                      } else {
                          Log.i("Debug", "Call not ended...");
                      }
                  } catch (ClassNotFoundException e) {
                      e.printStackTrace();
                  } catch (NoSuchMethodException e) {
                      e.printStackTrace();
                  } catch (InvocationTargetException e) {
                      e.printStackTrace();
                  } catch (IllegalAccessException e) {
                      e.printStackTrace();
                  } catch (RemoteException e) {
                      e.printStackTrace();
                  }
                  break;
              case TelephonyManager.CALL_STATE_IDLE:
                  Log.i("Debug", "IDLE");
                  break;
          }
      }
  }
  </code></pre>
  
  <p><strong>ITelephony.aidl</strong></p>
  
  <pre><code>package com.android.internal.telephony;    
  interface ITelephony {
      boolean endCall();
      void answerRingingCall();
      void silenceRinger();
  }
  </code></pre>