0

I want to disconnect incoming call programmatically, I searched regarding this, I found so many links on stack overflow. I tried all of them, but none of them worked

I created ITelephony class in package com.android.internal.telephony

This is the code in ITelephony class

The code I used is...

public class Incommingcall extends BroadcastReceiver{
    TelephonyManager manager;
    int callstate;
    private ITelephony telephonyService;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        try{
            manager = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);

            callstate = manager.getCallState();

        if(callstate == TelephonyManager.CALL_STATE_RINGING){
            Log.i("IncommingCall","Call is Ringing");
            Class c = Class.forName(manager.getClass().getName());
            Method m = c.getDeclaredMethod("getITelephony");
            Log.i("m= ", ""+m);
            m.setAccessible(true);
            telephonyService = (ITelephony) m.invoke(manager);

            telephonyService.endCall();
            Log.i("IncommingCall","Call Disconnected");
        }else if (callstate == TelephonyManager.CALL_STATE_IDLE) {

        }else if(callstate == TelephonyManager.CALL_STATE_OFFHOOK){

        }

        }catch(Exception e){

        }

    }

}

In the manifest file

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />

The Logcat I got is...

08-04 13:57:48.686: I/IncommingCall(29302): Call is Ringing
08-04 13:57:48.686: I/m=(29302): private com.android.internal.telephony.ITelephony android.telephony.TelephonyManager.getITelephony()
Cœur
  • 37,241
  • 25
  • 195
  • 267
nawaab saab
  • 1,892
  • 2
  • 20
  • 36
  • [According to the docs](http://developer.android.com/reference/android/Manifest.permission.html#MODIFY_PHONE_STATE), that permission is not meant to be used by third party apps. Unless you're implementing your own ROM, I don't think you're going to make it work. – mdelolmo Aug 04 '14 at 08:52
  • @mdelolmo this permission is granted to system apps but we can also use this in our apps, look over this [link](http://stackoverflow.com/a/14334194/3409600) – nawaab saab Aug 04 '14 at 09:04

2 Answers2

0

You have to use PhonestateListener plese check below way

    private class MyPhoneStateListener extends PhoneStateListener {
    public void onCallStateChanged(int state, String incomingNumber) {


        switch (state) {
        case TelephonyManager.CALL_STATE_RINGING:
            // Disconnect the call here...
            try {
                TelephonyManager manager = (TelephonyManager) mContext
                        .getSystemService(Context.TELEPHONY_SERVICE);
                Class c = Class.forName(manager.getClass().getName());
                Method m = c.getDeclaredMethod("getITelephony");
                m.setAccessible(true);
                ITelephony telephony = (ITelephony) m.invoke(manager);
                telephony.endCall();


            } catch (Exception e) {
            }

            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            break;
        case TelephonyManager.CALL_STATE_IDLE:
            break;

        }
    }
}

start lisener in reciever as follows

TelephonyManager tmgr;    
MyPhoneStateListener PhoneListener = new MyPhoneStateListener()
tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
pavanmvn
  • 749
  • 10
  • 21
  • I tried this It didn't worked, even i have n't got this log case TelephonyManager.CALL_STATE_RINGING: // Disconnect the call here... try { Log.i("Call", "Incomming call"); – nawaab saab Aug 04 '14 at 09:21
0

I added these two permissions and now its working fine

<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS" />
nawaab saab
  • 1,892
  • 2
  • 20
  • 36