Blocking Incoming call - Android

Step 1:
Create Broadcast receiver class for incoming call

package com.javaorigin.android.sample;

import java.lang.reflect.Method;

import com.android.internal.telephony.ITelephony;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent
import android.telephony.TelephonyManager;
import android.util.Log;

public class PhoneCallReceiver extends BroadcastReceiver {
 Context context = null;
 private static final String TAG = "Phone call";
 private ITelephony telephonyService;

 @Override
 public void onReceive(Context context, Intent intent) {
  Log.v(TAG, "Receving....");
  TelephonyManager telephony = (TelephonyManager) 
  context.getSystemService(Context.TELEPHONY_SERVICE);  
  try {
   Class c = Class.forName(telephony.getClass().getName());
   Method m = c.getDeclaredMethod("getITelephony");
   m.setAccessible(true);
   telephonyService = (ITelephony) m.invoke(telephony);
   //telephonyService.silenceRinger();
   telephonyService.endCall();
  } catch (Exception e) {
   e.printStackTrace();
  }
  
 }

 
}


Step 2:
Create IDL interface for getting core Telephony service
package name must be com.android.internal.telephony

FileName : ITelephony.aidl
  package com.android.internal.telephony;

  interface ITelephony {

   
    boolean endCall();

  
    void answerRingingCall();

   
    void silenceRinger();

  }


Step 3:
AndroidManifest.xml configuration
  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.javaorigin.android.sample"
      android:versionCode="1"
      android:versionName="1.0">  
    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <receiver  android:name=".PhoneCallReceiver">
            <intent-filter  android:priority="100" >
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>

    </application>
    <uses-sdk android:minSdkVersion="5" />
    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <uses-sdk android:minSdkVersion="8" />

</manifest>

54 comments :

  1. AIDL file also seems to be java. But I am unable to understand the naming convention which Google followed. Thanks for your useful article.

    ReplyDelete
    Replies
    1. after rejecting the call i am send an sms to the person who is called to me but i noticed that the above code is executing more the once help whats the problem is.

      Delete
  2. hi,

    i have tried this code as it is but in my project it will not work.
    please help me i am doing R & d on this topic from last three weeek.


    With thanks
    jeet

    ReplyDelete
  3. пожалуйста вышлите мне на ящик код проекта alezhk@gmail.com

    ReplyDelete
  4. hai.....I am senthil...student from Pondicherry University....I followed this tutorial perfectly.....when I was running i got

    " Neither user 10034 nor current process has android.permission.MODIFY_PHONE_STATE"

    error.... Can you Help me to come out of this error...??

    ReplyDelete
  5. This code does't work ,,, please suggest me ..

    ReplyDelete
  6. Senthil,
    Please add MODIFY_PHONE_STATE permission in your manifest file

    ReplyDelete
    Replies
    1. I get same error on htc phone android 2.3
      " Neither user 10008 nor current process has android.permission.MODIFY_PHONE_STATE"

      Delete
    2. This comment has been removed by the author.

      Delete
    3. It's because since android 2.3 this permission is a system level permission. See http://stackoverflow.com/questions/4715250/how-to-grant-modify-phone-state-permission-for-apps-ran-on-gingerbread for more details.

      Delete
    4. when i am adding MODIFY_PHONE_STATE permision inside mainfest file i got error like that Permission is only granted to system apps,
      what is the solution of this problm please reply....

      Delete
  7. package com.mmc.com;

    import java.lang.reflect.Method;

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.telephony.TelephonyManager;
    import android.util.Log;
    import android.widget.ListView;
    import android.widget.Toast;

    import com.android.internal.telephony.ITelephony;
    public class PhoneCallReceiver extends BroadcastReceiver {
    Context context = null;
    private static final String TAG = "Phone call";
    private ITelephony telephonyService;
    DBAdapter _dbconnection;

    protected Cursor cursor;
    @Override
    public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Log.i(TAG, "Receving....");
    TelephonyManager telephony = (TelephonyManager)

    context.getSystemService(Context.TELEPHONY_SERVICE);


    try {
    Bundle bundle = intent.getExtras();
    String phoneNr= bundle.getString("incoming_number");
    Class c = Class.forName(telephony.getClass().getName());


    Method m = c.getDeclaredMethod("getITelephony");
    m.setAccessible(true);
    Log.i("Caling", phoneNr);
    Toast.makeText(context,phoneNr, Toast.LENGTH_LONG).show();
    _dbconnection=new DBAdapter(context);
    _dbconnection.open();
    cursor= _dbconnection.M_CheckBlockNumber(phoneNr);


    Toast.makeText(context,String.valueOf(cursor.getCount()), Toast.LENGTH_LONG).show();

    if(cursor.getCount()>0)
    {

    if (cursor.moveToFirst()) {
    do {
    // c.getString(0)
    Toast.makeText( context,"id: " + cursor.getString(2), Toast.LENGTH_LONG).show();
    //IsHaveAccount="1";
    cursor.getString(2);

    } while (cursor.moveToNext());
    }


    Toast.makeText(context,"Call End", Toast.LENGTH_LONG).show();
    telephonyService = (ITelephony) m.invoke(telephony);
    telephonyService.silenceRinger();
    telephonyService.endCall();

    }
    else
    {

    Toast.makeText(context,"New ", Toast.LENGTH_LONG).show();
    telephonyService = (ITelephony) m.invoke(telephony);
    telephonyService.silenceRinger();
    telephonyService.endCall();
    Toast.makeText(context,"Call End", Toast.LENGTH_LONG).show();
    }

    cursor.close();
    _dbconnection.close();

    } catch (Exception e) {

    Toast.makeText(context,e.getMessage(), Toast.LENGTH_LONG).show();
    e.printStackTrace();
    }
    }


    }

    ReplyDelete
  8. can we get the incoming number in above code ? i want to send sms to the rejected call ? How can it possible ?

    ReplyDelete
  9. Thank you very mush for the code, This code had help me a lot. I want an icon in the menu with some setting. e.g. add Number to be blocked.

    Thank you

    ReplyDelete
    Replies
    1. Bundle b = intent.getExtras();
      number = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

      Delete
  10. Great article about Blocking Incoming Calls - android! I guess lot of people glad to read this code. This is very helpful, especially in having this problem. Thanks for sharing!

    ReplyDelete
  11. only you need to delete this line telephonyService.silenceRinger(); and you program
    will be bug free i tes this on 2.3 also working well
    Thanks

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. This Code is Succesfully Run on Emulator But not Run in Mobile ...
    Can AnyBody Help me PLZ...

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  14. Excellent post duuuude.. keep it up..! Thanks a lot...

    ReplyDelete
  15. Hi this is shashi from hyderabad, its successively running in lower versions but not on higher versions of android can you help me out so that it should run on all versions of android.

    Thanks and regards.

    ReplyDelete
  16. i Cant add ITelephony.aidl in a package..It shows "ITelephony.aidl" is not a valid java identifier..Help me

    ReplyDelete
  17. Just checking on HTC One V with 4.0.3 giving exception as same as above mentioned "java.lang.SecurityException: Neither user 10147 nor current process has android.permission.MODIFY_PHONE_STATE."
    Any Suggestion how program-matically I can answer a call in android ?

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  18. Just checking on Samsung Galaxy GT-I9000 OS 2.2.1 its working fine,anyone can tell me what's the reason why its not working on HTC with OS 4.0.3 ? I am using the same code for both test (test for HTC and Samsung)

    ReplyDelete
  19. Replies
    1. This comment has been removed by the author.

      Delete
    2. Try to add 'android.permission.CALL_PHONE' to your manifest file

      it worked for me :)

      Delete
  20. can any one suggest
    why its not working on 4.1
    its just working on 2.2

    ReplyDelete
  21. hi sir:
    i have tested this toturial for 2.2 on emulator MODIFY_PHONE_STATE is giving me error that this is system pemission..

    i have removed this permission from minifest.
    now my app crashes when i call to emulator .

    give solution for this.
    thanks in advance.

    ReplyDelete
  22. HTC Sensation with 4.0.3 comes back with "java.lang.SecurityException: Neither user 10147 nor current process has android.permission.MODIFY_PHONE_STATE." Does anyone have a solution? MODIFY_PHONE_STATE is added to mainfest and endCall is working.

    ReplyDelete
  23. dear i have a problem in my code, i have made a application for call block any number.
    if i do block a number and when i busy with other call then if blocked number call me
    then all calls are close. please tell me splution when blocked number call me then not distrub my others call.

    ReplyDelete
  24. Works great!!!
    Tested on my S3.
    Once you comment the silenceRinger function the security exception goes off.
    Very helpful code.

    ReplyDelete
  25. This comment has been removed by the author.

    ReplyDelete
  26. I have a black list, i check the incoming number if it is in there then end the call. Everything works well except a small problem. It does not ring on the first call(sometimes even the second) but rings on the third, fourth... and the calling screen appear for half of a second before the call ends. Anyone has a solution for this?

    ReplyDelete
  27. hi i have one doubt how to make a call and i want to know call_lifting state call_reject state please help me

    ReplyDelete
  28. @admin
    I am not able to create a file in eclipse with this extension.

    ".aidl"
    What to do now please help

    ReplyDelete
    Replies
    1. Before creating this you will create a package and name will be always com.android.internal.telephony ..after this follow this method
      Righet Click on Project ->got to File-> Save file with .aidl and Finish... After this copy code and paste in it....

      Delete
  29. This comment has been removed by the author.

    ReplyDelete
  30. I m getting an error at DbAdaptor.
    Is it needed to create a DbAdaptor class manually?

    ReplyDelete
  31. work good but still record a call log. :( any one help me

    ReplyDelete
  32. how to acess set reject messages progrmeticaly.

    ReplyDelete
  33. Endcall() seems end all calls.
    If I has a active call, then i has another incoming call which is black list (Enable Call waiting), i use callend(), and it will end all calls include active call.
    Any suggestion about this issue?
    Thanks!

    ReplyDelete
  34. these tutorials are very good for beginners as well as the advanced learners. Thanks
    Pawan Kumar
    http://www.sourcecodehub.com

    ReplyDelete
  35. I've done it using Android Shell, the technique works in following sitautions

    Incoming call, which is in ringing state Outgoing call, which might be ringing on the other end or not Connected call, while you're talking be it dialled by you or the other party

    Try this through ADB Shell to drop/disconnect a call on your phone or emulator

    service call phone 5
    Detailed answer, explanation, and a little background will be lengthy for posting here so I've placed it on my blog. Check it out: http://aprogrammersday.blogspot.com/2014/05/disconnect-block-drop-calls-android-4.html

    ReplyDelete
  36. Starting a business is very easy task. On the off chance that you need to figure out how to make money on the web, you have to have great business plans.

    ReplyDelete