Android端(等待呼叫)程序化
我正在为呼叫中心的人构建某种类型的util应用程序。我必须在等待呼叫时执行操作 假设ABC试着打电话给我,我接了电话,同时XYZ也试着打电话给我,那么XYZ呼叫将移动到等待状态,直到我做出一些决定,如结束ABC呼叫或拒绝XYZ呼叫 到目前为止,我能够检测到ABC何时与我交谈,XYZ何时打电话给我——我也会收到同样的事件。在这种情况下,如果我调用/运行disconnectCall方法,那么我的第一个调用是get end up,但我希望自动结束第二个调用,而不是第一个调用 查看下面我的代码,该代码检测正在等待的呼叫过多onNewIncomingCallReceived当新呼叫进入等待状态时,会添加此日志Android端(等待呼叫)程序化,android,broadcastreceiver,telephonymanager,incoming-call,Android,Broadcastreceiver,Telephonymanager,Incoming Call,我正在为呼叫中心的人构建某种类型的util应用程序。我必须在等待呼叫时执行操作 假设ABC试着打电话给我,我接了电话,同时XYZ也试着打电话给我,那么XYZ呼叫将移动到等待状态,直到我做出一些决定,如结束ABC呼叫或拒绝XYZ呼叫 到目前为止,我能够检测到ABC何时与我交谈,XYZ何时打电话给我——我也会收到同样的事件。在这种情况下,如果我调用/运行disconnectCall方法,那么我的第一个调用是get end up,但我希望自动结束第二个调用,而不是第一个调用 查看下面我的代码,该代码检
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();
}
}
有谁能帮我自动结束第二次通话,然后用户已经在和其他人通话了。在过去的三天里,我一直在寻找解决方案,但我没有得到它