Blocking a call without user intervention in android with an example

I was very much in search of the way in which we can control calls in Android and faced a lot of difficulties implementing it at the beginning. So I thought to share some knowledge about the same which I gained in the journey.

Now, let’s start.

First create a package in the ‘src’ folder in your project named com.android.internal.telephony and within that package create a file and copy paste the interface ITelephony(I’ve given the interface below) and save the file as ITelephony.aidl. When you compile the project you will get a corresponding java file for the ITelephony in the ‘gen’ folder.

Code for ITelephony.aidl:

package com.android.internal.telephony;
interface ITelephony {

 boolean endCall();

 void answerRingingCall();

 void silenceRinger();
}

com.android.internal.telephony is an internal hidden class in the Android Telephony framework. As com.android.internal.telephony is not a public class in the sdk, we use java reflections for retrieving the internal class’s methods.

Now let’s see the permissions needed in the Manifest file:

<uses-permission android:name=”android.permission.READ_PHONE_STATE”/>
<uses-permission android:name=”android.permission.MODIFY_PHONE_STATE”/>
<uses-permission android:name=”android.permission.CALL_PHONE”/>

Now let’s look at the XML file where I’ve added a checkbox enabling which would block the incoming calls.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >
<CheckBox
 android:id="@+id/cbBlockAll"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Block all Calls" />
</LinearLayout>

First, we need to use a BroadcastReceiver which responds to the incoming call with action android.intent.action.PHONE_STATE  to detect the incoming call and also we need a PhoneStateListener  to listen to the call state and check whether the state is CALL_STATE_RINGING. If yes, then end the call using endcall() method.

BroadcastReceiver CallBlocker;

To get the Telephony services,

TelephonyManager telephonyManager;

To get the ITelephony methods:

ITelephony telephonyService;

Now look at the code snippet below which blocks calls when the checkbox is enabled.

blockAll_cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

 @Override
 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
 // TODO Auto-generated method stub
 CallBlocker =new BroadcastReceiver()
 {
 @Override
 public void onReceive(Context context, Intent intent) {
 // TODO Auto-generated method stub
 telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
 //Java Reflections
 Class c = null;
 try {
 c = Class.forName(telephonyManager.getClass().getName());
 } catch (ClassNotFoundException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 Method m = null;
 try {
 m = c.getDeclaredMethod("getITelephony");
 } catch (SecurityException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (NoSuchMethodException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 m.setAccessible(true);
 try {
 telephonyService = (ITelephony)m.invoke(telephonyManager);
 } catch (IllegalArgumentException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (IllegalAccessException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (InvocationTargetException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 telephonyManager.listen(callBlockListener, PhoneStateListener.LISTEN_CALL_STATE);
 }//onReceive()
 PhoneStateListener callBlockListener = new PhoneStateListener()
 {
 public void onCallStateChanged(int state, String incomingNumber)
 {
 if(state==TelephonyManager.CALL_STATE_RINGING)
 {
 if(blockAll_cb.isChecked())
 {
 try {
 telephonyService.endCall();
 } catch (RemoteException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 }
 }
 }
 };
 };//BroadcastReceiver
 IntentFilter filter= new IntentFilter("android.intent.action.PHONE_STATE");
 registerReceiver(CallBlocker, filter);
 }
 });

Here we use java reflections to get the instance of com.android .intenal.telephony class.

The code snippet is shown below:

Class c = null;
 try {
 c = Class.forName(telephonyManager.getClass().getName());
 } catch (ClassNotFoundException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 Method m = null;
 try {
 m = c.getDeclaredMethod("getITelephony");
 } catch (SecurityException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (NoSuchMethodException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 m.setAccessible(true);
 try {
 telephonyService = (ITelephony)m.invoke(telephonyManager);
 } catch (IllegalArgumentException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (IllegalAccessException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (InvocationTargetException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }

Now use the internal methods in the internal class to block calls as shown below:

Here endcall() is used to end a call without user intervention.

telephonyService.endCall();

Register the receiver as shown below:

IntentFilter filter= new IntentFilter("android.intent.action.PHONE_STATE");
 registerReceiver(CallBlocker, filter);

We need to unregister the receiver after use. This is done in the onDestroy() callback.

protected void onDestroy() {
 // TODO Auto-generated method stub
 super.onDestroy();
 if (CallBlocker != null)
 {
 unregisterReceiver(CallBlocker);
 CallBlocker = null;
 }
 }

Now the whole code:

package com.deepthi.mycallcontroller;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import com.android.internal.telephony.ITelephony;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Bundle;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.telephony.PhoneNumberUtils;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class MyCallControllerActivity extends Activity {
 /** Called when the activity is first created. */
 CheckBox blockAll_cb;//,blockcontacts_cb;
 BroadcastReceiver CallBlocker;
 TelephonyManager telephonyManager;
 ITelephony telephonyService;
@Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 initviews();
 blockAll_cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

 @Override
 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
 // TODO Auto-generated method stub
 CallBlocker =new BroadcastReceiver()
 {
 @Override
 public void onReceive(Context context, Intent intent) {
 // TODO Auto-generated method stub
 telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
 //Java Reflections
 Class c = null;
 try {
 c = Class.forName(telephonyManager.getClass().getName());
 } catch (ClassNotFoundException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 Method m = null;
 try {
 m = c.getDeclaredMethod("getITelephony");
 } catch (SecurityException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (NoSuchMethodException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 m.setAccessible(true);
 try {
 telephonyService = (ITelephony)m.invoke(telephonyManager);
 } catch (IllegalArgumentException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (IllegalAccessException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (InvocationTargetException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 telephonyManager.listen(callBlockListener, PhoneStateListener.LISTEN_CALL_STATE);
 }//onReceive()
 PhoneStateListener callBlockListener = new PhoneStateListener()
 {
 public void onCallStateChanged(int state, String incomingNumber)
 {
 if(state==TelephonyManager.CALL_STATE_RINGING)
 {
 if(blockAll_cb.isChecked())
 {
 try {
 telephonyService.endCall();
 } catch (RemoteException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 }
 }
 }
 };
 };//BroadcastReceiver
 IntentFilter filter= new IntentFilter("android.intent.action.PHONE_STATE");
 registerReceiver(CallBlocker, filter);
 }
 });
}
 public void initviews()
 {
 blockAll_cb=(CheckBox)findViewById(R.id.cbBlockAll);
 //blockcontacts_cb=(CheckBox)findViewById(R.id.cbBlockContacts);
 }
 @Override
 protected void onDestroy() {
 // TODO Auto-generated method stub
 super.onDestroy();
 if (CallBlocker != null)
 {
 unregisterReceiver(CallBlocker);
 CallBlocker = null;
 }
 }
}

Now the Screen Shots:

Hope this helped 🙂 Happy Coding 🙂

Advertisements

, , , , , , , , , ,

  1. #1 by Dhanish on September 25, 2012 - 5:01 pm

    it is not working….

  2. #2 by Michal on November 5, 2012 - 5:04 pm

    Hi there, I would like to ask which versions of android is this hack capable to run in. I have experience that it works in under 2.3 and in 2.3 in emulator, but not in a real device… could you please specify which versions of OS were tested against this hack? Currently my app contains this implementation and works in 2.3 in emulator but not in real device… thank you

  3. #3 by Rashi.. Coorg.. on November 7, 2012 - 12:11 pm

    Very nice example and easily understood.. 🙂 Thanks a lot.. 🙂 When i worked out this, it worked fine in emulator, but when i tried with a real device ( 2.3 Version), it dint work for me.. I dont understand what the reason behind as am a beginner.. If u dont mind can u pls get me the reason pls..

    • #4 by Michal on November 9, 2012 - 9:47 pm

      please read my comment above… I have the same situation

  4. #5 by Samadhan on November 28, 2012 - 12:38 am

    this code gives error when an incoming call. plz send me full src code on this e-mail id samadhanmedge@gmail.com.
    plz send the src code….

  5. #6 by maleek on January 8, 2013 - 9:41 am

    dear deepthi,
    since im a beginner i would like to ask you,
    how do I replace the checkbox with a shared preference checkbox?, I have created the preference but couldnt load it inside on phonestatelistener.
    thank you

  6. #7 by Dzmitry on February 7, 2013 - 2:02 am

    Hi all. I just test it on my Samsung Galaxy S Plus (GT9001) with installed Android 4.1.2 and it’s work very well.
    Thanks a lot for this solution.
    It realy great to get such mechanism to manipulate with telephony.

    • #8 by Niño on October 17, 2013 - 6:59 am

      I am pretty curious to know how this method can work since Android 2.3 (in which the ”android.permission.MODIFY_PHONE_STATE” is allow only for system Apps and not anymore for 3rd Party Apps) Do you use a custom ROM ? Have you tried it on other devices running upper than Froyo (2.2) ?

  7. #9 by http://yahoo.com on February 11, 2013 - 7:21 pm

    “Blocking a call without user intervention in android with an example Android Desk”
    honestly enables myself ponder a small amount extra.
    I cherished every single section of it. Thank you -Caren

  8. #10 by cbhatt02 on February 16, 2013 - 9:19 am

    its working thanks a lot u saved us :))
    thanks again:)

  9. #11 by dparedes02 on February 28, 2013 - 9:58 pm

    Hi, I need the ITelephony.aidl, where donwload??

  10. #12 by rishi on March 13, 2013 - 5:59 pm

    u r great 🙂 its working…

  11. #13 by Ansuha on March 18, 2013 - 5:39 pm

    Hi,I want block incoming calls through my app.Any one can send me the source code.
    anu.g.chitti@gmail.com this is my mail id.

  12. #14 by bhk on March 30, 2013 - 4:21 pm

    thanks that helped 🙂

  13. #15 by VyNLN on April 5, 2013 - 7:36 pm

    please send to me your source code completed. thanks. nguyenlengocvy@gmail.com

  14. #16 by Hasala on August 2, 2013 - 11:10 am

    Hi,

    In my application I need to start a call when I press a button and then terminate the call after some specific time. (eg: 15 seconds)
    Do you know how to achieve this?

    Thanks in advance,
    Hasala

  15. #17 by hari.S.babu on September 5, 2013 - 6:58 pm

    can we have the full prpject code to study and understand. because, the snippets above are not well formated. and, as this is a complicated thing we need to understand and we neeed to use t in our own works with some modifications. you can send it to kinghari010@gmail.com I have come up with an idea like this but in a different settings. thank you.

  16. #18 by hari.S.babu on September 6, 2013 - 11:29 am

    when I try to ssave the Itelephonya s aidle, It is giving this Fatal Error
    interface ITelephony should be declared in a file called com\android\internal\telephony\ITelephony.aidl.

    how to solve this? It is better to give a link here to study it>

  17. #19 by hari.S.babu on September 6, 2013 - 3:53 pm

    thanks for the code. It is working for me. but to make that interface as aidl is not neccessory. I just added it as normal interface and it is working.

  18. #20 by Sulaiman Khan on October 29, 2013 - 1:32 pm

    thanks for your article, please send me your code on sulaimankhanuet@yahoo.com

  19. #21 by mahsa on October 31, 2013 - 4:15 pm

    please send to me your source code completed. thanks. azizymahsa@yahoo.com

  20. #22 by Anand on December 20, 2013 - 4:19 pm

    please send to me your source code completed. thanks vickyhemnani@gmail.com

  21. #23 by Shayan Aryan on January 31, 2014 - 8:39 pm

    thanks, It worked! It’s a nice solution till android unhide this API

  22. #24 by Gautam mama on February 14, 2014 - 3:20 pm

    Its not Working. Its showing a lot of errors. please get back on this and help us. vforvizzy@gmail.com

  23. #25 by ohara on April 30, 2014 - 7:01 am

    Wow It’s greate !!
    Thank you 🙂

  24. #26 by G on May 14, 2014 - 7:40 pm

    Please send to me your source code completed. thanks. grzjur@poczta.onet.pl

  25. #27 by suporte on May 22, 2014 - 4:22 am

    The most important issue when it comes to communication via the internet is the
    security of the data transmitted. Thhe sense and ability to collaborate are one of the key drivers to busuness success.
    See how thiss author explores both the good and the bad of Vo – IP as a
    business solution.

  26. #28 by Vivek Rathaur on May 24, 2014 - 10:58 pm

    Ohh. BRAVO……………. thanks a lot. thanx .

  27. #29 by prithvi chauhan on May 27, 2014 - 3:23 pm

    It works thank you

  28. #30 by afif on May 27, 2014 - 6:20 pm

    how to blocking class without using checkbox???

  29. #31 by Aman on July 22, 2014 - 1:43 am

    How do u block outgoing call??

  30. #32 by hossein on July 28, 2014 - 5:29 pm

    send me src plz s.h.akbari435@gmail.com

  1. end incoming call programmatically | SuperBlog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: