2011-06-20 65 views
10

我正在致力於一個Android應用程序,我想要 能夠撥打電話,但有一個非常精確的限制,這是 「致電未接來電」。我想要的是,能夠掛斷電話剛開始振鈴時的 。如何撥打未接來電?

現在我可以知道手機什麼時候開始嘗試撥打 的電話,但幾秒鐘之後就沒有「振鈴」活動,這是我願意做的。

我該如何阻止這個確切的時刻?

回答

2

通過PhoneStateListener使用onCallStateChanged()你只能檢測當手機開始撥出電話當呼出hunged了,但你不能確定時, 「響鈴」開始。我試了一次,查看下面的代碼:

撥出時撥出的呼叫從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; 
     } 

    } 



}