Search code examples
androidtelephonymanagerandroid-audiomanager

Android: Use TelephonyManager or AudioManager for checking phone state before executing code


My question is, what is the best way to get the phone state (is it in call mode), to decide the further way. I would like to check for phone state before deciding whether execute sound notification or not. If I understood right the TelephonyManager requires a BroadcastReceiver so app can detect when the phone state has changed. But in my app I want to check phone state in one discreet moment.


Solution

  • I don't think there's a best way because there is only one way, check the call state via TelephonyManager. No need for a receiver:

    TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    int callState = tm.getCallState(); 
    

    See this for the call state constants.