My requirement is Accept and Disconnect a incoming call.
It is working fine If Call is Accepted and then wait for 3 seconds(handler.postDelayed) and then Disconnecting.
But I wanna disconnect the call immediately after accepting.(Dont wanna wait for 3 sconds)
Here is my code. Your help is appreciated.
public void acceptAndDissConnectCall(final Context activity) {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
Log.i(TAG, "It is LOLLIPOP or Greater====");
answerCall(activity);
} else{
// do something for phones running an SDK before lollipop
Log.i(TAG, "This device is prior to LOLLIPOP ====");
acceptCall(activity);
}
dissConnectCall(activity);
}
public boolean dissConnectCall(final Context activity) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
try {
Log.d(TAG, "Dissconnect call ===== ");
// Get the boring old TelephonyManager
TelephonyManager telephonyManager = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);
// Get the getITelephony() method
Class classTelephony = Class.forName(telephonyManager.getClass().getName());
Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");
// Ignore that the method is supposed to be private
methodGetITelephony.setAccessible(true);
// Invoke getITelephony() to get the ITelephony interface
Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);
// Get the endCall method from ITelephony
Class telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName());
Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");
// Invoke endCall()
methodEndCall.invoke(telephonyInterface);
} catch (Exception ex) { // Many things can go wrong with reflection calls
ex.printStackTrace();
// return false;
}
// return true;
}
}, 4000);
return false;
}
public static void acceptCall(Context context){
Log.d(TAG, "accept the call ====");
Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonUp.putExtra(Intent.EXTRA_KEY_EVENT,
new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
context.sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
}
public void answerCall(final Context mContext) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Log.d(TAG, "Answer the call ====");
Runtime.getRuntime().exec("input keyevent " + Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));
} catch (IOException e) {
Log.e(TAG, "IOException on answerCall ========== ");
// Runtime.exec(String) had an I/O problem, try to fall back
String enforcedPerm = "android.permission.CALL_PRIVILEGED";
Intent btnDown = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
Intent btnUp = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
mContext.sendOrderedBroadcast(btnDown, enforcedPerm);
mContext.sendOrderedBroadcast(btnUp, enforcedPerm);
}
}
}).start();
}