I'm building some sort of util app for Call Center people. Where I've to perform action on waiting call.
Suppose ABC tries to call me and I answered call, and same time XYZ also tries to call me then XYZ call will moved to waiting state untill I make some dicision like end call with ABC or reject XYZ call.
Till now I'm able to detect when ABC is talking with me and XYZ calls me- I'm getting the event for the same. when in that event case if i call/run disconnectCall method then my first call is getting endup but I want to end second call automatically instead of first one.
Check out my code below which detect waiting call too onNewIncomingCallReceived this log is added when new call is placed into waiting.
public class CallListenerReceiver extends BroadcastReceiver {
//The receiver will be recreated whenever android feels like it. We need a static variable to remember data between instantiations
private static int lastState = TelephonyManager.CALL_STATE_IDLE;
private static Date callStartTime;
private static boolean isIncoming;
private boolean isUserOnCall = false;
private static final String TAG = "CallListenerReceiver";
private static String savedNumber; //because the passed incoming is only valid in ringing
@Override
public void onReceive(Context context, Intent intent) {
//We listen to two intents. The new outgoing call only tells us of an outgoing call. We use it to get the number.
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
} else {
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
int state = 0;
if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
state = TelephonyManager.CALL_STATE_IDLE;
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
state = TelephonyManager.CALL_STATE_OFFHOOK;
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
state = TelephonyManager.CALL_STATE_RINGING;
}
onCallStateChanged(context, state, number);
}
}
//Incoming call- goes from IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when its hung up
//Outgoing call- goes from IDLE to OFFHOOK when it dials out, to IDLE when hung up
public void onCallStateChanged(Context context, int state, String number) {
if (lastState == state) return;
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
callStartTime = new Date();
savedNumber = number;
if (lastState == TelephonyManager.CALL_STATE_OFFHOOK) {
showToast(context, "New Call In Waiting");
Log.d(TAG, "onNewIncomingCallReceived : ");
disconnectCall(context);
} else {
showToast(context, "Call Received");
Log.d(TAG, "onIncomingCallReceived : ");
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//Transition of ringing-> offhook are pickups of incoming calls. Nothing done on them
if (lastState != TelephonyManager.CALL_STATE_RINGING) {
isIncoming = false;
callStartTime = new Date();
Log.d(TAG, "onOutgoingCallStarted : ");
} else {
isUserOnCall = true;
isIncoming = true;
callStartTime = new Date();
showToast(context, "Call Answered");
Log.d(TAG, "onIncomingCallAnswered : ");
}
break;
case TelephonyManager.CALL_STATE_IDLE:
//Went to idle- this is the end of a call. What type depends on previous state(s)
if (lastState == TelephonyManager.CALL_STATE_RINGING) {
//Ring but no pickup- a miss
showToast(context, "onMissedCall");
Log.d(TAG, "onMissedCall : ");
} else if (isIncoming) {
isUserOnCall = false;
showToast(context, "onIncomingCallEnded");
Log.d(TAG, "onIncomingCallEnded : ");
} else {
Log.d(TAG, "onOutgoingCallEnded : ");
}
break;
}
lastState = state;
}
private void disconnectCall(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
try {
TelecomManager telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
if (telecomManager != null)
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ANSWER_PHONE_CALLS) == PackageManager.PERMISSION_GRANTED)
telecomManager.endCall();
else
showToast(context, "Permission not available to disconnect call");
} catch (Exception e) {
try {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (tm == null) return;
tm.getClass().getMethod("endCall").invoke(tm);
} catch (Exception se) {
}
}
} else endCall(context);
}
private void endCall(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Method m = tm.getClass().getDeclaredMethod("getITelephony");
m.setAccessible(true);
com.android.internal.telephony.ITelephony telephonyService = (com.android.internal.telephony.ITelephony) m.invoke(tm);
telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
}
}
private void showToast(Context context, String toastMessage) {
Toast.makeText(context, toastMessage, Toast.LENGTH_LONG).show();
}
}
Can anyone help me with to end the second call automatically then user user is already talking with someone else. I'm finding solution for this last 3 days but I didn't get it.