9

I am creating an app to reject calls from specific numbers without even getting a single ring to the calling person.

I have a code that rejects the call after a partial ring. Please don't say this question is repeated. I have been searching code to reject the call without a ring for long time still didn't find the solution. Kindly help me!

public void onReceive(Context context, Intent intent) {
    Bundle b = intent.getExtras();
    incommingNumber = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

     String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);


        for(int i=0;i<blockedNumbers.length;i++)
        {
            if(incommingNumber.equalsIgnoreCase(blockedNumbers[i]))
            {
                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();
                  }
            }
        }

    }

This is the code I have used to reject the call. But it rejects with one ring.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user1537462
  • 197
  • 3
  • 9
  • 1
    http://stackoverflow.com/questions/7121508/android-taking-complete-control-of-phone-is-it-possible-how/7121586#7121586 – Vineet Shukla Nov 12 '12 at 10:12

3 Answers3

1

you have to define priority in manifest.. for example:

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

I believe you have to call:

setResultData(null); 

By doing so you "kill" the message being passed on through all the receivers.

If this doesn't help your should try to find a way to give your receiver a higher priority in the system, so that you can take over and set the result data to null for all the subsequent receiver calls.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
DecodeGnome
  • 1,809
  • 1
  • 19
  • 36
0

Use these permissions in android manifest file;

<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" />

The first permission "MODIFY_PHONE_STATE" is very important, because this handles disabling Phone Ringing during call. When anybody calls you , at that time your phone will show a FLASH TYPE of the incoming no. with no ringing.

This work at my end. Do tell me if found any problem.