15

I want to reject incoming in android, I have seen so many code from these links.

  1. Android: Taking complete control of phone(kiosk mode), is it possible? How?

  2. How to Reject a call programatically in android

  3. http://androidsourcecode.blogspot.in/2010/10/blocking-incoming-call-android.html

  4. http://androidbridge.blogspot.com/2011/05/how-to-answer-incoming-call-in-android.html?showComment=1361478035090#c5468022990217431781

But I am still unable to do it, can anybody tell me in simple and easy steps how to do it?

Community
  • 1
  • 1
Zubair
  • 304
  • 1
  • 5
  • 19
  • I intercepted outgoing call, but unable to understand the logic behind incoming calls and their interceptions mentioned on these pages. – Zubair Feb 21 '13 at 21:09
  • possible duplicate of [End call in android programmatically](http://stackoverflow.com/questions/18065144/end-call-in-android-programmatically) – Flow Aug 28 '14 at 10:05

6 Answers6

17

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.

Axe
  • 6,285
  • 3
  • 31
  • 38
Zubair
  • 304
  • 1
  • 5
  • 19
5

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....");

        }
Community
  • 1
  • 1
Ajay Pandya
  • 2,417
  • 4
  • 29
  • 65
  • Great solutions .. Need to put one add on to it ... For M+ versions we are also required to take run time permission for CALL_PHONE. Cheers! – AndroidHacker Oct 04 '16 at 07:09
  • Have put the complete solution over self asked question.. http://stackoverflow.com/questions/39527748/programmatically-disconnect-call-in-android-marshmallow-version – AndroidHacker Oct 04 '16 at 07:10
2

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.

Ariel Magbanua
  • 3,083
  • 7
  • 37
  • 48
  • lol..I have been trying for 4 to 5 days..thought may there be something that is missed in these links.. – Zubair Feb 21 '13 at 21:18
  • 1
    Another tip, if you want the phone calls will be rejected only if the app is running, better register the receiver programmatically in onResume cycle and unregister it onPause cycle. Because I think if you will register it on the manifest it is registered as long as the app is not removed in your system, and it will capture or receive phone calls even the app is not running. – Ariel Magbanua Feb 21 '13 at 21:43
  • Hmm. that's good point. But what if I am using services? I should unregister it on destroy()? – Zubair Feb 21 '13 at 21:52
  • You cannot unregister services, call stopService method instead and do it onPause() cycle because I think onDestroy() is not guaranteed to be called by the system. – Ariel Magbanua Feb 21 '13 at 22:15
1

Create an interface in com.android.internal.telephony and name as

interface ITelephony {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
    boolean endCall();
    void silenceRinger();strong text
    void answerRingingCall();
}

//   then catch the incoming call intent and call below method



  if (intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER) != null) {
                        number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                        Toast.makeText(context, "INCOMING call catched: ", Toast.LENGTH_SHORT).show();
disconnectPhoneItelephony(mContext)
                    }


    private void disconnectPhoneItelephony(Context context) {
            ITelephony telephonyService;
            TelephonyManager telephony = (TelephonyManager)
                    context.getSystemService(Context.TELEPHONY_SERVICE);
            try {
                Class c = Class.forName(telephony.getClass().getName());
                Method m = c.getDeclaredMethod("getITelephony");
                m.setAccessible(true);
                SessionManager.getInstance(context).setBlockStatusAllow("BLOCKED");
                telephonyService = (ITelephony) m.invoke(telephony);
                telephonyService.silenceRinger();
                telephonyService.endCall();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

Add necessary permission in Manifest

Randy
  • 9,419
  • 5
  • 39
  • 56
Ramesh Kanuganti
  • 275
  • 1
  • 11
1

This is the best tutorial to block all incoming calls you can customize it to block incoming call of only selected numbers.

Android Incoming Call Block

0

If you are going to build a kiosk app then i suggest you to obtain notification settings and put device to Do not Disturb mode ON.

var mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

mNotificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE)