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="http://schemas.android.com/apk/res/android"
   package="com.example.callsrejected"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk
     android:minSdkVersion="8"
     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"/>
   <application
     android:allowBackup="true"
     android:icon="@drawable/ic_launcher"
     android:label="@string/app_name"
     android:theme="@style/AppTheme" >
     <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
           <action android:name="android.intent.action.MAIN" />

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

</manifest>


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.media.AudioManager;
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);

      }
   }

  if(!incomingNumber.equals("")){
   // 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
        rejectCall();
      // Send the rejected message ton app
       startApp(context,incomingNumber);
      }

   }
}


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

private void rejectCall(){

      
try {

// Get the getITelephony() method
Class<?> classTelephony = Class.forName(telephonyManager.getClass().getName());     
Method method = classTelephony.getDeclaredMethod("getITelephony");
// Disable access check
    method.setAccessible(true);
     // 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()
        methodEndCall.invoke(telephonyInterface);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    
}
}



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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.example.callsrejected.MainActivity" >

   <TextView
     android:id="@+id/txtmessage"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textColor="#FF1111"
     android:textSize="20sp"
     android:layout_centerInParent="true"
    />

</RelativeLayout>


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.support.v7.app.ActionBarActivity;
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 {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     // Get intent object sent from the IncomingCallReceiver
     Intent intent=getIntent();
     Bundle b=intent.getExtras();
     TextView tv=(TextView)findViewById(R.id.txtmessage);
     if(b!=null){
     // Display rejected number in the TextView    
     tv.setText(b.getString("number"));
     }

   }

}

Android-reject-call

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.