You'll need some Reflection for that. First, create this interface:
package com.android.internal.telephony;
public interface ITelephony {
boolean endCall();
void answerRingingCall();
void silenceRinger();
}
And end call with this:
private void endCall() {
TelephonyManager telephonyManager = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
Class<?> myClass = null;
try {
myClass = Class.forName(telephonyManager.getClass().getName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Method method = null;
try {
method = myClass.getDeclaredMethod("getITelephony");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
method.setAccessible(true);
ITelephony telephonyService = null;
try {
telephonyService = (ITelephony) method.invoke(telephonyManager);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
telephonyService.endCall();
}
Modified answer from here.
EDIT: Adding example:Add this to some button click
//Start call
makeCall(phoneNumber);
//wait several seconds
try {
long time = holdCallSeconds * 1000;
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
//hang up
endCall();
void makeCall(String phoneNumber) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(phoneNumber));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
startActivity(intent);
}