title: How to programmatically answer/end a call in Android 4.1?
body: <p>I am writing an android app in which I need to answer an incoming call, do some work and then end the call. 
  After all the Googling I could find two different ways to achieve this both of which do not work with recent versions of Android, specifically after 4.1, Jelly Bean. </p>
  
  <p>I.) Access "com.android.internal.telephony.ITelephony" using Java Reflection in the Broadcast receiver for "android.intent.action.PHONE_STATE". Below sample code can be found in hundreds of related post:</p>
  
  <pre><code>public class PhoneCallReceiver extends BroadcastReceiver {
   Context context = null;
   private static final String TAG = "Phone call";
   private ITelephony telephonyService;
  
  @Override
   public void onReceive(Context context, Intent intent) {
    if (!intent.getAction().equals("android.intent.action.PHONE_STATE")) 
      return;
  
    Log.v(TAG, "Receving....");
    TelephonyManager telephony = (TelephonyManager) 
    context.getSystemService(Context.TELEPHONY_SERVICE);  
    try {
        Log.v(TAG, "Get getTeleService...");
        Class c = Class.forName(telephony.getClass().getName());
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        telephonyService = (ITelephony) m.invoke(telephony);
        telephonyService.silenceRinger();
        Log.v(TAG, "Answering Call now...");
        telephonyService.answerRingingCall();
        Log.v(TAG, "Call answered...");
        //telephonyService.endCall();
    } catch (Exception e) {
     e.printStackTrace();
     Log.e(TAG,
             "FATAL ERROR: could not connect to telephony subsystem");
     Log.e(TAG, "Exception object: " + e);
    }
   }
  }
  </code></pre>
  
  <p>The problem with this code is that </p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /&gt; 
  </code></pre>
  
  <p>is required for this method to work, and this permission has been defined as "for system apps only" from android v 2.3. In short, normal user apps can not define this permission in the manifest file anymore. </p>
  
  <p>II.) Another way is to simulate pushing of the Headset hook which makes Android answer the call. This is done by broadcasting the "Intent.ACTION_MEDIA_BUTTON" as shown in below code.</p>
  
  <pre><code>public class PhoneCallReceiver extends BroadcastReceiver {
   Context context = null;
   private static final String TAG = "Phone call";
  
   @Override
   public void onReceive(Context context, Intent intent) {
       if (!intent.getAction().equals("android.intent.action.PHONE_STATE")) 
           return;
  
       String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
       if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
           String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
  
           Intent answer = new Intent(Intent.ACTION_MEDIA_BUTTON);
           answer.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
           context.sendOrderedBroadcast(answer, null);
           Log.d(TAG, "Answered incoming call from: " + number);
       }   
       return;
   } 
  }
  </code></pre>
  
  <p>This method works till Android 4.1 after which android has restricted user apps from broadcasting "Intent.ACTION_MEDIA_BUTTON". </p>
  
  <p>So my conclusion is that currently there is no way how we can achieve this in Android 4.1 or later. </p>
  
  <p>Has anybody else found any other solution or workaround to this problem?</p>