如何拨打未接来电?

我正在开发一个Android应用程序,我希望能够打电话,但却有一个非常严格的限制,这就是“打错电话”。 我想要的是,只要电话开始响起,就能挂断电话。

现在我能够知道手机什么时候开始尝试拨打电话,但是几秒钟之内没有networking上的“振铃”活动,这是我愿意做的。

我怎样才能阻止这个确切的时刻?

Solutions Collecting From Web of "如何拨打未接来电?"

通过PhoneStateListener使用onCallStateChanged(),您只能检测到电话何时开始拨打电话,以及何时打出电话,但无法确定“响铃”何时开始。 我试了一次,看看下面的代码:

呼出时,从IDLE开始拨号到OFFHOOK,当呼出时拨到IDLE。 唯一的解决方法是在打出电话之后使用定时器挂断电话,并在几秒钟后通过,但不能保证电话将开始振铃。

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; } } } 

在PhoneStateListener中使用onCallStateChanged()