Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reject a call programmatically without a ring

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.

like image 474
user1537462 Avatar asked Nov 12 '12 09:11

user1537462


1 Answers

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>
like image 62
user3312793 Avatar answered Oct 20 '22 19:10

user3312793