Rejecting Incoming call in android Rejecting Incoming call in android android android

Rejecting Incoming call in android


In order to intercept your call you just have to:
1. Make a package named. com.android.internal.telephony;
2. In this package make a interface file named ITelephony.
and write this code in that interface file.

boolean endCall();void answerRingingCall();void silenceRinger();

Now in your class where you want to intercept the call extend that class to BroadcastReceiver and in onReceive()function write the following code.

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

Thats it.


Provide appropriate permission and add the ITelephony.java file

private void declinePhone(Context context) throws Exception {        try {            String serviceManagerName = "android.os.ServiceManager";            String serviceManagerNativeName = "android.os.ServiceManagerNative";            String telephonyName = "com.android.internal.telephony.ITelephony";            Class<?> telephonyClass;            Class<?> telephonyStubClass;            Class<?> serviceManagerClass;            Class<?> serviceManagerNativeClass;            Method telephonyEndCall;            Object telephonyObject;            Object serviceManagerObject;            telephonyClass = Class.forName(telephonyName);            telephonyStubClass = telephonyClass.getClasses()[0];            serviceManagerClass = Class.forName(serviceManagerName);            serviceManagerNativeClass = Class.forName(serviceManagerNativeName);            Method getService = // getDefaults[29];            serviceManagerClass.getMethod("getService", String.class);            Method tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder.class);            Binder tmpBinder = new Binder();            tmpBinder.attachInterface(null, "fake");            serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder);            IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone");            Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class);            telephonyObject = serviceMethod.invoke(null, retbinder);            telephonyEndCall = telephonyClass.getMethod("endCall");            telephonyEndCall.invoke(telephonyObject);        } catch (Exception e) {            e.printStackTrace();            Log.d("unable", "msg cant dissconect call....");        }


You will be needing a broadcast receiver for that with an intent filter I think ACTION_ANSWER

more details about it here: http://developer.android.com/reference/android/content/Intent.html#ACTION_ANSWER

You can register it using the manifest file or the method registerReceiver

more info here: http://developer.android.com/reference/android/content/Context.html#registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter)

Also you need to specify appropriate permissions 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" /><uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS" />

The links you have provided are quite good. You just need more patience :)

happy coding.