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

  31. #33 by chandru on September 2, 2014 - 11:18 am

    i need to find for if i reject the call or accept the call how to know that one. please if u know that please send me the code chandbecse@gmail.com . but if one person call me if i reject the call state is idle state and who is call me he is reject the call that also is idle state. and if i attend the call and after i end the call state it’s also idle state …i have so many problem please help me..
    if any one know the code please call me. 9677089975

  32. #34 by lansana on September 13, 2014 - 4:19 am

    hello,
    send me source code please lansanalsm@gmail.com

  33. #35 by Ếch Oop on November 23, 2014 - 5:17 pm

    please send me src code completed, thanks… phamtan04101991@gmail.com

  34. #36 by g2-25b36ae4fe8e92095fd65c3d41af06c4 on November 28, 2014 - 3:51 pm

    plz send me the source code on vasim.shk@gmail.com

  35. #37 by gianni on November 29, 2014 - 8:16 am

    Hi,

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

    Thanks in advance,
    Gianni

  36. #38 by Rahul thakur on April 7, 2015 - 12:50 pm

    it’s not working so please send this code on my email…

  37. #39 by Mayank Shah on April 8, 2015 - 3:34 pm

    helped me a lot

  38. #40 by minhvu on April 8, 2015 - 10:47 pm

    Please send to me your source code completed. thanks. conduongthanhdat0702@gmail.com

  39. #41 by PANKAJ CHAUHAN on April 9, 2015 - 6:16 pm

    plz send me full code of this example
    pjchauhan1432@gmail.com
    plzzzz

  40. #42 by selva on June 5, 2015 - 1:49 pm

    how to create block call app using service

  41. #43 by Tiago on July 15, 2015 - 3:09 pm

    Nice it works with 4.1.2 version (Samsung Galaxy S2 – not rooted). I was wondering if it worked in lollipoop version for any of you! Thanks!

    • #44 by Tiago on July 15, 2015 - 4:09 pm

      sorry I meant rooted device. Have not tested in a non rooted phone yet!

  42. #45 by AC on January 20, 2016 - 2:18 pm

    How to display the incoming number when ringing? Is there any hints to block some specific numbers?

  43. #46 by sarathi on February 3, 2016 - 10:27 am

    hi plz send the source code to my id. sarathi1989@gmail.com

  44. #47 by fateme on July 9, 2016 - 12:57 am

    Hi
    Thank you , It is very useful for me but I have a problem to add Ithelephony.
    can you send me it’s source code?

  45. #48 by Paranoyaq on March 22, 2017 - 5:47 pm

    Please send to me your source code completed. thanks. quiet1day@gmail.com

  46. #49 by Abhinav kashyap on July 26, 2017 - 3:15 pm

    hello , i try this code it not work on Android 6.0 please send me code on kashyapabhinav2013@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: