2014-08-28 70 views
0

我正在開發一個應用程序,當我開始通話並且它還設置揚聲器模式時,會啓動鬧鐘(鈴聲)。所以我想在我打電話的人接聽電話時停止鬧鐘。我怎麼能做到這一點?當其他人接聽電話時,是否可以知道使用TelephonyManager(onCallStateChanged)?

這是我的代碼:

private PhoneStateListener phoneCallListener = new PhoneStateListener() { 

    private int prevCallState=TelephonyManager.CALL_STATE_IDLE; 

    public void onCallStateChanged(int state, String incomingNumber) { 
     try { 
      AudioManager audiomanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
      switch (state) { 
       case TelephonyManager.CALL_STATE_OFFHOOK: 
        if (prevCallState == TelephonyManager.CALL_STATE_IDLE){       
         //Inicio de llamada saliente        
         audiomanager.setMode(AudioManager.MODE_IN_CALL); 
         audiomanager.setSpeakerphoneOn(true); 

        } 
        break; 
       case TelephonyManager.CALL_STATE_IDLE: 
        if (prevCallState == TelephonyManager.CALL_STATE_OFFHOOK){ 
         //Fin de llamada saliente 
         audiomanager.setSpeakerphoneOn(false); 
        } 
        break;     
      } 
     } catch (Exception e) { 

     } 
    prevCallState = state; 
    } 
}; 

我希望得到任何幫助。感謝很多。

回答

0

我有類似的問題,這是不可能的檢查,如果該人紛紛拿起手機和他/她的手機都無法到達。電話公司在GSM網絡中沒有任何反饋。

0
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    button = (Button) findViewById(R.id.buttonCall); 

    // add PhoneStateListener 
    PhoneCallListener phoneListener = new PhoneCallListener(); 
    TelephonyManager telephonyManager = (TelephonyManager) this 
     .getSystemService(Context.TELEPHONY_SERVICE); 
    telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE); 

    // add button listener 
    button.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      Intent callIntent = new Intent(Intent.ACTION_CALL); 
      callIntent.setData(Uri.parse("tel:+9196203333")); 
      startActivity(callIntent); 

     } 

    }); 

} 

//monitor phone call activities 
private class PhoneCallListener extends PhoneStateListener { 

    private boolean isPhoneCalling = false; 

    String LOG_TAG = "LOGGING 123"; 

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

     if (TelephonyManager.CALL_STATE_RINGING == state) { 
      // phone ringing 
      Log.i(LOG_TAG, "RINGING, number: " + incomingNumber); 
     } 

     if (TelephonyManager.CALL_STATE_OFFHOOK == state) { 
      // active 
      Log.i(LOG_TAG, "OFFHOOK"); 

      isPhoneCalling = true; 
     } 

     if (TelephonyManager.CALL_STATE_IDLE == state) { 
      // run when class initial and phone call ended, 
      // need detect flag from CALL_STATE_OFFHOOK 
      Log.i(LOG_TAG, "IDLE"); 

      if (isPhoneCalling) { 

       Log.i(LOG_TAG, "restart app"); 

       // restart app 
       Intent i = getBaseContext().getPackageManager() 
        .getLaunchIntentForPackage(
         getBaseContext().getPackageName()); 
       i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(i); 

       isPhoneCalling = false; 
      } 

     } 
    } 
} 

,並給予許可,不得以艙單

+0

我想確切地知道當對方接聽電話,但我認爲@tamasJozsa是正確的,有沒有辦法讓那個狀態 – Carl 2014-08-28 07:58:57

相關問題