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. #3 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. #4 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..

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

      please read my comment above… I have the same situation

  4. #6 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. #7 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. #8 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.

    • #9 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. #10 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. #11 by cbhatt02 on February 16, 2013 - 9:19 am

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

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

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

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

    u r great 🙂 its working…

    • #14 by Aftab Ali on May 26, 2013 - 7:09 am

      Rishi Can you send me the complete code on this email id Please
      taurus_ishere@hotmail.com.
      Did you tested it on real device and if you did can you tell me on which version as i need this to work on latest versions also

    • #15 by prakash on June 13, 2013 - 3:41 pm

      did u get answerRingingCall() to work? I couldnt. So please help

  11. #16 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. #17 by bhk on March 30, 2013 - 4:21 pm

    thanks that helped 🙂

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

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

  14. #19 by Muhammad Jamshaid Khan on May 3, 2013 - 6:34 pm

    this application working correct but how call block from specific number?

  15. #20 by Pankaj kumar gupta on May 13, 2013 - 11:12 am

    it’s working ………………. 🙂
    Thanks a lot.

  16. #21 by Aftab Ali on May 26, 2013 - 7:10 am

    Can you send me the complete code on this email id Please
    taurus_ishere@hotmail.com.
    Did you tested it on real device and if you did can you tell me on which version as i need this to work on latest versions also

  17. #22 by Toan Nguyen on May 30, 2013 - 9:45 am

    plz send me full src code on this e-mail id dinhtoan.1988@hotmail.com Thanks

  18. #23 by Mr.Bean on May 30, 2013 - 1:23 pm

  19. #24 by prakash on June 13, 2013 - 3:33 pm

    Hi,

    Thanks very much for the tutorial.I tried your example. But it works only for ending call. If i try answering call it throws an error

    java.lang.SecurityException: Neither user 10110 nor current process has android.permission.MODIFY_PHONE_STATE.
    Now, I know that android doesnt allow users to use that permission for their apps. So how can i achieve answer call feature??

    Thanks for the post and keep up the awesome work:)

  20. #25 by kumanan on July 2, 2013 - 2:49 pm

    Please send me the full source code to my mail id. kumskoolguy@gmail.com. Thanks in advance

  21. #26 by sahil on July 13, 2013 - 12:48 pm

    thanks for the help, i was working so hard …….

  22. #27 by sahil on July 13, 2013 - 12:51 pm

    can u tell me how to start a service in user specified time. only in between that time service should run

  23. #28 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

  24. #29 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.

  25. #30 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>

  26. #31 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.

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

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

  28. #33 by mahsa on October 31, 2013 - 4:15 pm

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

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

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

  30. #35 by Shayan Aryan on January 31, 2014 - 8:39 pm

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

  31. #36 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

  32. #37 by ohara on April 30, 2014 - 7:01 am

    Wow It’s greate !!
    Thank you 🙂

  33. #38 by G on May 14, 2014 - 7:40 pm

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

  34. #39 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.

  35. #40 by Vivek Rathaur on May 24, 2014 - 10:58 pm

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

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

    It works thank you

  37. #42 by afif on May 27, 2014 - 6:20 pm

    how to blocking class without using checkbox???

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

    How do u block outgoing call??

  39. #44 by hossein on July 28, 2014 - 5:29 pm

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

  40. #45 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

  41. #46 by lansana on September 13, 2014 - 4:19 am

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

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

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

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

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

  44. #49 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

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

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

  46. #51 by Mayank Shah on April 8, 2015 - 3:34 pm

    helped me a lot

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

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

  48. #53 by PANKAJ CHAUHAN on April 9, 2015 - 6:16 pm

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

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

    how to create block call app using service

  50. #55 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!

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

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

  51. #57 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?

  52. #58 by sarathi on February 3, 2016 - 10:27 am

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

  53. #59 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?

  54. #60 by Paranoyaq on March 22, 2017 - 5:47 pm

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

  55. #61 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. block incomming call in android needn't permission android.permission.MODIFY_PHONE_STATE | BlogoSfera
  2. end incoming call programmatically | SuperBlog
  3. end incoming call programmatically | Ziler Answers

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: