2

I've used the following code to disconnect a call programatically but It's not working.

private void callDisconnect(){ 
    try{ 
        TelephonyManager manager = (TelephonyManager)this.getSystemService(this.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){ 
        Log.d("",e.getMessage()); 
    } 
}

Can anybody help me with this?? Do I need to change the code or something...??

Sam DeHaan
  • 10,246
  • 2
  • 40
  • 48
Bhagirath
  • 161
  • 3
  • 14

5 Answers5

5

Simply use this broadcastreceiver. I tested it in one of my application and it works perfectly.

public class MyBroadcastReceiver extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, final Intent intent)
    {


        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL))
        {
            String phoneNumber = intent.getExtras().getString(Intent.EXTRA_PHONE_NUMBER);

            if (phoneNumber.equals("123456"))
            {
                if (getResultData() != null)
                {

                    setResultData(null);
                }


            }

        }


    }

}
Keshav Verma
  • 51
  • 1
  • 3
5

For disconnecting a call programmatically you must add ITelephony.AIDL file in your project. If you have added it, then your package name must be com/android/internal/telephony/ITelephony.AIDL: for more information see Blocking Incoming call. Download the AIDL file from here.

To disconnect a call use endCall(); method of ITelephony

Dheeraj Vepakomma
  • 26,870
  • 17
  • 81
  • 104
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

You can block the outgoing call using the setResultData(null) function in the onReceive method of the Broadcast receiver.

public class BlockOutgoing extends BroadcastReceiver {

String number;
@Override
public void onReceive(Context context, Intent intent) 
{
    Log.d("12280", "asdasNumber is-->> " + number);
    number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
    setResultData(null);
    Toast.makeText(context, "Outgoing Call Blocked" , 5000).show();
}

}

   <receiver
        android:name=".BlockOutgoing"
        android:label="@string/app_name" >
        <intent-filter android:priority="1">

            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />

        </intent-filter>
    </receiver>
1

It is not possible anymore in newer versions of android. The user decides when to end the call if it has already started. You can however block calls from happening.

Akhil
  • 13,888
  • 7
  • 35
  • 39
-1

You cannot end a call like that in android 2.3+... using your code... I think only user can end his call...and for earlier versions You can see this link and This one

Community
  • 1
  • 1
5hssba
  • 8,079
  • 2
  • 33
  • 35