title: How can I accept/rejected incoming calls from android 5 and above?
body: <p>I need to listen to incoming calls. I use the services for this. My activity opens when a call comes in. There are 2 buttons there. I want to accept an incoming call when one of this button is pressed. I tried many things but none of them worked. I would be glad if you show any help or resources.</p>
  
  <blockquote>
    <p>AndroidManifest.xml</p>
  </blockquote>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.READ_CONTACTS" /&gt;
      &lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt;
      &lt;uses-permission android:name="android.permission.CALL_PHONE" /&gt;
      &lt;uses-permission android:name="android.permission.MANAGE_OWN_CALLS" /&gt;
      &lt;uses-permission android:name="android.permission.READ_CALL_LOG" /&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.WAKE_LOCK" /&gt;
      &lt;uses-permission android:name="android.permission.DISABLE_KEYGUARD" /&gt;
  
      &lt;uses-permission android:name="android.permission.ANSWER_PHONE_CALLS"/&gt;
      &lt;uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/&gt;
  
      &lt;application
  
          &lt;activity android:name=".Activities.IncomingActivity"&gt;
              &lt;intent-filter&gt;
                  &lt;data android:mimeType="vnd.android.cursor.item/phone" /&gt;
                  &lt;data android:mimeType="vnd.android.cursor.item/person" /&gt;
                  &lt;data android:mimeType="vnd.android.cursor.dir/calls" /&gt;
                  &lt;data android:scheme="tel" /&gt;
  
                  &lt;category android:name="android.intent.category.HOME" /&gt;
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
  
                  &lt;action android:name="android.intent.action.VIEW" /&gt;
                  &lt;action android:name="android.intent.action.DIAL" /&gt;
  
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
                  &lt;category android:name="android.intent.category.BROWSABLE" /&gt;
  
                  &lt;data android:mimeType="vnd.android.cursor.item/phone" /&gt;
                  &lt;data android:mimeType="vnd.android.cursor.item/person" /&gt;
                  &lt;data android:mimeType="vnd.android.cursor.dir/calls" /&gt;
                  &lt;data android:scheme="tel" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;receiver android:name="ixa.whocalled.BroadcastReceiver.PhoneListenerBroad"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.PHONE_STATE" /&gt;
                  &lt;action android:name="android.intent.action.NEW_OUTGOING_CALL" /&gt;
                  &lt;action android:name="android.intent.action.SCREEN_OFF"/&gt;
                  &lt;action android:name="android.intent.action.SCREEN_ON"/&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
  
      &lt;/application&gt;
  </code></pre>
  
  <blockquote>
    <p>PhoneListenerBroad -- *BroadcastReceiver</p>
  </blockquote>
  
  <pre><code>public class PhoneListenerBroad extends BroadcastReceiver {
  
      Context c;
      private String outgoing;
  
      @Override
      public void onReceive(Context context, Intent intent) {
          c = context;
  
          if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
              outgoing = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
              int state = 2;
              Intent intentPhoneCall = new Intent(c, IncomingActivity.class);
              intentPhoneCall.putExtra("incomingnumber", outgoing);
              intentPhoneCall.putExtra("state", state);
              intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              c.startActivity(intentPhoneCall);
  
              Toast.makeText(context, "test", Toast.LENGTH_SHORT).show();
          }
  
          try {
              TelephonyManager tmgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
              MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
              tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
          } catch (Exception e) {
              Log.e("Phone Receive Error", " " + e);
          }
  
      }
  
      private class MyPhoneStateListener extends PhoneStateListener {
          public void onCallStateChanged(final int state, final String incomingNumber) {
              Handler callActionHandler = new Handler();
              Runnable runRingingActivity = new Runnable() {
                  @Override
                  public void run() {
                      if (state == 1) {
                          Intent intentPhoneCall = new Intent(c, IncomingActivity.class);
                          intentPhoneCall.putExtra("incomingnumber", incomingNumber);
                          intentPhoneCall.putExtra("state", state);
                          intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                          c.startActivity(intentPhoneCall);
                      }
                  }
              };
  
              if (state == 1) {
                  callActionHandler.postDelayed(runRingingActivity, 100);
              }
  
              if (state == 0) {
                  callActionHandler.removeCallbacks(runRingingActivity);
              }
          }
      }
  }
  </code></pre>
  
  <blockquote>
    <p>IncomingActivity</p>
  </blockquote>
  
  <pre><code>public class IncomingActivity extends AppCompatActivity {
  
      Button Accept_Button, Decline_Button;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_incoming);
  
          Accept_Button = findViewById(R.id.ai_button01);
          Decline_Button = findViewById(R.id.ai_button02);
  
          Accept_Button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  acceptCall();
              }
          });
  
          Decline_Button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  //empty
              }
          });
      }
  
      public void acceptCall() {
  
          Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
          buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
          sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
  
          Intent headSetUnPluggedintent = new Intent(Intent.ACTION_HEADSET_PLUG);
          headSetUnPluggedintent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
          headSetUnPluggedintent.putExtra("state", 0);
          headSetUnPluggedintent.putExtra("name", "Headset");
          try {
              sendOrderedBroadcast(headSetUnPluggedintent, null);
          } catch (Exception e) {
              e.printStackTrace();
          }
  
      }
  }
  </code></pre>