问题描述
我正在为Call Center人员构建某种util应用程序。我必须在等待呼叫时执行操作的地方。
假设ABC试图打电话给我,我接了电话,而XYZ也试图打电话给我,那么XYZ呼叫将进入等待状态,直到我做出一些决定,例如用ABC结束呼叫或拒绝XYZ呼叫。
到目前为止,我能够检测到ABC在何时与我交谈,而XYZ打电话给我-我正在为同一事件进行活动。在这种情况下,如果我调用/运行disconnectCall方法,那么我的第一个电话将结束,但我想自动结束第二个电话,而不是第一个。
在下面的代码中检查我的代码,该代码也可以检测到等待的呼叫 onNewIncomingCallReceived 。当将新呼叫置于等待状态时,将添加此日志。
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();
}
}
谁能帮助我自动结束第二个电话,那么用户已经在与其他人通话。我最近三天都在寻找解决方案,但没有得到。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)