[android] 통화 중 상태 확인하기 – PhoneStateListener

@Override
protected void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
  manager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}

private PhoneStateListener phoneStateListener = new PhoneStateListener()
{
  public void onCallStateChanged(int state, String incomingNumber)
  {
    Log.i("MainActivity.java | onCallStateChanged", 
    "|state:" + state 
    + "(ringing:" + TelephonyManager.CALL_STATE_RINGING 
    + ", offhook:" + TelephonyManager.CALL_STATE_OFFHOOK 
    + ", idle:" + TelephonyManager.CALL_STATE_IDLE + ")" 
    + "|number:" + incomingNumber + "|");
  };
};

 

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

 

TelephonyManager.CALL_STATE_RINGING : 벨소리 울리는 중
TelephonyManager.CALL_STATE_OFFHOOK : 통화 시작
TelephonyManager.CALL_STATE_IDLE : 통화 끝, 벨소리 울리는 중에 통화 거절