10

In my app I will maintain a list of contacts.

Any calls from contacts in the list will be dropped. They will show under missed calls but the phone will not ring.

Brock Adams
  • 82,642
  • 19
  • 207
  • 268
harish
  • 245
  • 1
  • 3
  • 13
  • ContentObserver is word where your can best practice – Nikunj Patel Sep 08 '11 at 12:20
  • possible duplicate of [How to block calls in android](http://stackoverflow.com/questions/1083527/how-to-block-calls-in-android) – Brock Adams Nov 11 '11 at 02:27
  • `MODIFY_PHONE_STATE` [permission is for system apps only](http://stackoverflow.com/questions/4715250/how-to-grant-modify-phone-state-permission-for-apps-ran-on-gingerbread) (for android 2.3 and above). If you want to use app only for phone you have access to, you can [make your app system app](http://projectmaxs.org/documentation/systemapp.html) – tchelidze Feb 07 '17 at 13:12

2 Answers2

19

First create this Interface:

  public interface ITelephony {

        boolean endCall();

        void answerRingingCall();

        void silenceRinger();

  }

Then Create this class that extends BroadcastReceiver

public class IncomingCallReceiver extends BroadcastReceiver {
    private ITelephony telephonyService;
    private String blacklistednumber = "+458664455";

    @Override
    public void onReceive(Context context, Intent intent) {

       TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
       try {
         Class c = Class.forName(tm.getClass().getName());
         Method m = c.getDeclaredMethod("getITelephony");
         m.setAccessible(true);
         ITelephony telephonyService = (ITelephony) m.invoke(tm);
         Bundle bundle = intent.getExtras();
         String phoneNumber = bundle.getString("incoming_number");
         Log.e("INCOMING", phoneNumber);
         if ((phoneNumber != null) && phoneNumber.equals(blacklistednumber)) { 
            telephonyService.silenceRinger();
            telephonyService.endCall();
            Log.e("HANG UP", phoneNumber);
         }

       } catch (Exception e) {
         e.printStackTrace();
       }
}

This will only block that single phonenumber, but you get the point.

In your manifest add this:

<receiver android:name=".IncomingCallReceiver">
    <intent-filter android:priority="999">
        <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver>
<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" />
<uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS" />
Yoda
  • 15,011
  • 59
  • 173
  • 291
Finn Larsen
  • 2,236
  • 15
  • 26
2

Download the class of ITelephony from here.

Then put it in a package (make a new package) of com.android.internal.telephony. Then import the package to the appropriate class and for rejecting a call use the endCall() method

lord.garbage
  • 5,514
  • 4
  • 28
  • 51
user3451725
  • 113
  • 1
  • 11