2014-04-01 59 views
1

中接聽電話我只想在撥出電話已接聽時啓動計時器。像Nexus 5中的默認電話應用程序一樣。如何查找是否已在android

有沒有方法可以確定電話是否已被回答?

電話號碼被撥打後,我立即嘗試了電話聽衆,它得到CALL_STATE_OFFHOOK。所以這不是正確的方法?

有人能幫我弄清楚這個嗎?

謝謝!

+0

的[檢測是否呼出已回答]可能重複(http://stackoverflow.com/questions/2250455/detect-if-an-outgoing-呼叫已被回答) – Sree

回答

0

可能這一個幫助你:

public class CallLogReceiver extends BroadcastReceiver { 

static boolean missedCall, incomingCall; 

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 


    if (intent.getAction().equals("android.intent.action.PHONE_STATE")) { 
     String state = intent.getExtras().getString(
       TelephonyManager.EXTRA_STATE); 

     if ((state != null) 
       && (state.equals(TelephonyManager.EXTRA_STATE_RINGING))) { 
      CallLogReceiver.phoneNo = intent.getExtras().getString(
        "incoming_number"); 
      CallLogReceiver.missedCall = true; 
     } 

     if ((state != null) 
       && (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))) { 
      CallLogReceiver.incomingCall = true; 

     } 
     if (state != null 
       && !(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) 
       && !(state.equals(TelephonyManager.EXTRA_STATE_RINGING))) { 

      if (CallLogReceiver.missedCall && CallLogReceiver.incomingCall) { 
       //incoming call logic 
      } 

      else if (CallLogReceiver.missedCall 
        && !CallLogReceiver.incomingCall) { 
       // missed call logic 
      } 
      CallLogReceiver.missedCall = CallLogReceiver.incomingCall = false; 
     } 
     } 
    } 
} 
+0

我仍然有同樣的問題 - 我按下撥號後馬上轉到state_OFFHOOK。 – TheDevMan

+0

不會有你的代碼有問題,因爲當你撥打電話號碼,然後狀態進入EXTRA_STATE_RINGING,當你拿起然後狀態進入EXTRA_STATE_OFFHOOK ...嘗試調試 –