Android reject call

In this Android tip, I am going to show you how to reject an incoming call programmatically. In the example application below, when there is an incoming call, the incoming number is checked in the contacts list. If the number does not match any number in the contacts list, it will be rejected.
To retrieve numbers from the contacts list stored in an Android phone, you can use the query() method of the ContentResolver. You will work with  ContactsContract.CommonDataKinds.Phone.CONTENT_URI content provider. It stores contact numbers.
To reject an incoming call, you will invoke the endCall() method of the ITelephony interface. The ITelephony interface can be obtained by calling the getITelephony() method of the TelophonyManager class.
Now to have an example application on rejecting an incoming call, you create a new Android project called CallsRejected. In the AndroidManifest.xml file, you need a receiver to receive the incoming call. The permissions for phone calls and contacts access also need to be added to the AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android=""
   android:versionName="1.0" >

     android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
     android:theme="@style/AppTheme" >
        android:label="@string/app_name" >
           <action android:name="android.intent.action.MAIN" />

           <category android:name="android.intent.category.LAUNCHER" />
     <receiver android:name=".IncommingCallReceiver">
   <intent-filter android:priority="999">
     <action android:name="android.intent.action.PHONE_STATE" />


Then in the src folder, you create the IncomingCallReceiver extending BroadcastReceiver to receive the incoming call and reject it if the incoming number does not match any number in the contacts list.

  package com.example.callsrejected;
import java.lang.reflect.Method;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.telephony.TelephonyManager;

public class IncomingCallReceiver extends BroadcastReceiver{
String incomingNumber="";
AudioManager audioManager;
TelephonyManager telephonyManager;

public void onReceive(Context context, Intent intent) {
  // Get AudioManager
  audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
  // Get TelephonyManager
   telephonyManager= (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
   if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
      String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
      if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
      // Get incoming number
         incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);


   // Get an instance of ContentResolver
    ContentResolver cr=context.getContentResolver();
    // Fetch the matching number
    Cursor numbers=cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID,ContactsContract.CommonDataKinds.Phone.NUMBER}, ContactsContract.CommonDataKinds.Phone.NUMBER +"=?", new String[]{incomingNumber}, null);
    if(numbers.getCount()<=0){ // The incoming number is not found in the contacts list
      // Turn on the mute
       audioManager.setStreamMute(AudioManager.STREAM_RING, true);
      // Reject the call
      // Send the rejected message ton app


private void startApp(Context context, String number){
     Intent intent=new Intent(context,MainActivity.class);
     intent.putExtra("number", "Rejected incoming number:"+number);

private void rejectCall(){

try {

// Get the getITelephony() method
Class<?> classTelephony = Class.forName(telephonyManager.getClass().getName());     
Method method = classTelephony.getDeclaredMethod("getITelephony");
// Disable access check
     // Invoke getITelephony() to get the ITelephony interface
        Object telephonyInterface = method.invoke(telephonyManager);
        // Get the endCall method from ITelephony
        Class<?> telephonyInterfaceClass =Class.forName(telephonyInterface.getClass().getName());
        Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");
        // Invoke endCall()
} catch (Exception e) {
// TODO Auto-generated catch block

In the activity_main.xml file, you have to add a TextView to display the rejected number when the application opens.

<RelativeLayout xmlns:android=""
   tools:context="com.example.callsrejected.MainActivity" >



Here is the content of the MainActivity class to receive the rejected number and display it in the TextView when there is an incoming call.

  package com.example.callsrejected;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

   protected void onCreate(Bundle savedInstanceState) {
     // Get intent object sent from the IncomingCallReceiver
     Intent intent=getIntent();
     Bundle b=intent.getExtras();
     TextView tv=(TextView)findViewById(;
     // Display rejected number in the TextView    




Posted by: Dara | post date: 11-02-2014 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.