我正在开发一个 Android 应用,希望能够进行通话,但有一个非常特殊的限制,即“漏接电话”。我的要求是,在手机开始响铃时就能挂断。
目前,我能够知道手机何时开始尝试拨打电话,但在接下来的几秒钟内,网络上没有“响铃”活动,这正是我想要的。
如何精确地停止电话呼叫?
我正在开发一个 Android 应用,希望能够进行通话,但有一个非常特殊的限制,即“漏接电话”。我的要求是,在手机开始响铃时就能挂断。
目前,我能够知道手机何时开始尝试拨打电话,但在接下来的几秒钟内,网络上没有“响铃”活动,这正是我想要的。
如何精确地停止电话呼叫?
public abstract class PhoneCallReceiver extends BroadcastReceiver {
static CallStartEndDetector listener;
@Override
public void onReceive(Context context, Intent intent) {
savedContext = context;
if(listener == null){
listener = new CallStartEndDetector();
}
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
public class CallStartEndDetector extends PhoneStateListener {
int lastState = TelephonyManager.CALL_STATE_IDLE;
boolean isIncoming;
public PhonecallStartEndDetector() {}
//Incoming call- IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when hung up
//Outgoing call- from IDLE to OFFHOOK when dialed out, to IDLE when hunged up
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
if(lastState == state){
//No change
return;
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
//incoming call started
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//Transition of ringing->offhook are pickups of incoming calls. Nothing down on them
if(lastState != TelephonyManager.CALL_STATE_RINGING){
isIncoming = false;
//outgoing call started
}
break;
case TelephonyManager.CALL_STATE_IDLE:
//End of call(Idle). The type depends on the previous state(s)
if(lastState == TelephonyManager.CALL_STATE_RINGING){
// missed call
}
else if(isIncoming){
//incoming call ended
}
else{
//outgoing call ended
}
break;
}
lastState = state;
}
}
}