2013-10-22 133 views
2

我有以下代碼,並且我已經閱讀了撥出電話當兩方之間的電話連接時,振鈴狀態處於活動狀態,並且其他方可以實際看到您正在呼叫並應答您的呼叫。空閒狀態是當呼叫斷開時,OFF_HOOK狀態是您按下按鈕進行撥號的時刻。我進入OFF_HOOK和IDLE狀態,但無法進入振鈴狀態。可能是什麼問題? 。任何幫助將不勝感激。振鈴狀態撥打電話

public void onReceive(final Context context, Intent intent) { 
      TelephonyManager Tm=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
      Tm.listen(new PhoneStateListener(){ 
       public void onCallStateChanged(int state,String number) { 
        super.onCallStateChanged(state, number); 
        switch (state) { 
         case TelephonyManager.CALL_STATE_IDLE: 
          //when Idle i.e no call 
          Toast.makeText(context, "Phone state Idle", 1).show(); 

          break; 
         case TelephonyManager.CALL_STATE_OFFHOOK: 

          //when Off hook i.e in call 
          //Make intent and start your service here 
          Toast.makeText(context, "Phone state Off hook", 1).show(); 

          break; 
         case TelephonyManager.CALL_STATE_RINGING: 
          Toast.makeText(context, "Phone state Ringing", 1).show(); 
         break; 


         default: 
          break; 
         } 
        }  

      },PhoneStateListener.LISTEN_CALL_STATE); 

回答

3

TelephonyManager.CALL_STATE_RINGING是指來電。它不適用於打出電話的其他方。

TelephonyManager.CALL_STATE_RINGING將在您有來電並且您的手機正在振鈴時觸發。

也供您參考。沒有公開的api來檢測對方打電話的響鈴狀態。

+0

所以沒有辦法來檢測時,對方應答了呼叫? – user2137186

+0

正如我所告訴的..有沒有這個公共api ..你可以檢查內部apis .. – stinepike

0

也許嘗試這個輔助類,我在網上抄的地方,不記得在哪裏

/*Helper class to detect incoming and outgoing calls.*/ 
public class CallHelper { 
    private Activity activity; 
    private TelephonyManager tm; 
    private CallStateListener callStateListener; 
    private OutgoingReceiver outgoingReceiver; 
    public String TelephoneNumber; 
    public boolean InCall = false; 

    public CallHelper(Activity activity) { 
     this.activity = activity; 
     callStateListener = new CallStateListener(); 
     outgoingReceiver = new OutgoingReceiver(); 
    } 

    /*Listener to detect incoming calls.*/ 
    private class CallStateListener extends PhoneStateListener { 

     @Override 
     public void onCallStateChanged(int state, String incomingNumber) { 
      switch (state) { 
       case TelephonyManager.CALL_STATE_RINGING: { 
        TelephoneNumber = incomingNumber; 
        inout = "in"; 
        InCall = false; 
        break; 
       } 
       case TelephonyManager.CALL_STATE_OFFHOOK: { 
        if (!InCall) { 
         InCall = true; 
         //Do your stuff here when in call... 
        } 
        break; 
       } 
       case TelephonyManager.CALL_STATE_IDLE: { 
        InCall = false; 
        break; 
       } 
       default: { 
        InCall = false; 
        break; 
       } 
      } 
     } 
    } 

    /*Broadcast receiver to detect the outgoing calls.*/ 
    public class OutgoingReceiver extends BroadcastReceiver { 
     public OutgoingReceiver() { } 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      TelephoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 
     } 
    } 

    /*Start calls detection.*/ 
    public void start() { 
     tm = (TelephonyManager) activity.getSystemService(Activity.TELEPHONY_SERVICE); 
     tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE); 

     IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL); 
     activity.registerReceiver(outgoingReceiver, intentFilter); 
    } 

    /*Stop calls detection.*/ 
    public void stop() { 
     tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE); 
     activity.unregisterReceiver(outgoingReceiver); 
    } 
}