2012-03-14 56 views
2

我需要一種方式來獲得撥出呼叫時的狀態。然而,在OFFHOOK狀態下,我也用來呼叫傳出呼叫(ACTION_CALL)。如何在不覆蓋傳出呼叫活動的情況下添加awnsered狀態?如何撥打電話在Android中得到解答

public class OutgoingBroadcastReceiver extends BroadcastReceiver { 

private Intent mIntent; 
private String phonenumber = null; 
public static boolean wasRinging; 

@Override 
public void onReceive(Context context, Intent intent) { 
    mIntent = intent; 

    MyPhoneStateListener phoneListener = new MyPhoneStateListener(context); 
    TelephonyManager telephony = (TelephonyManager) context 
      .getSystemService(Context.TELEPHONY_SERVICE); 
    telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE); 

    phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 

} 

public class MyPhoneStateListener extends PhoneStateListener { 
    private final Context context; 

    public MyPhoneStateListener(Context context) { 
     this.context = context; 

    } 

    @Override 
    public void onCallStateChanged(int state, String incomingNumber) { 


     switch (state) { 

     case TelephonyManager.CALL_STATE_IDLE: 

      wasRinging = true; 
      break; 

     case TelephonyManager.CALL_STATE_OFFHOOK: 

       Log.e("%%%%%%%%%%%%%%%%%%%%%%%%%%%t", "OFFHOOK"); 
      if (UIUDialer.isOutgoingCall() == true) { 

       //Do my work when outgoing call is detected 
      } 

      else if (!wasRinging) 
      { 

        Log.e("%%%%%%%%%%%%%%%%%%%%%%%%%%%t", "WASRINGING"); 
       //Do my work when outgoing call is awnsered 

      } 



      context.sendBroadcast(new Intent("finish_incoming")); 
      wasRinging = true; 
      break; 

     case TelephonyManager.CALL_STATE_RINGING: 

      wasRinging = true; 
      break; 
     } 
    } 

} 

}

+0

狀態'CALL_STATE_RINGING'被**從來不叫**在'OUTGOING_CALL',所以變量wasRinging將始終設置好的假。 – Houcine 2013-05-29 11:00:30

回答

0

你爲什麼不使用boolean wasRinging?
(你必須讓靜態和刪除初始化,雖然)

+0

感謝安德烈亞斯對你的幫助和時間。我根據你的建議進行了編輯。我忘了告訴你,我的PhoneStateListner是我的廣播接收器類的內部類。看到我的日誌貓,當一個傳出的電話被回答時,它不會進入OFFHOOK ..有意義嗎? – user1163234 2012-03-15 08:52:40

+0

wasinging必須在您的MyPhoneStateListener類 – Andreas 2012-03-15 10:42:07

+0

我的班級可以最終?公共靜態類MyPhoneStateListener擴展PhoneStateListener {0}私有最終上下文上下文{ 私有最終上下文上下文; private static boolean wasRinging; – user1163234 2012-03-15 12:32:57