SoFunction
Updated on 2025-04-11

Android music player monitors phone status implementation code


 private boolean mResumeAfterCall = false;
 private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
  @Override
  public void onCallStateChanged(int state, String incomingNumber) {
   if (state == TelephonyManager.CALL_STATE_RINGING) {
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int ringvolume = audioManager
      .getStreamVolume(AudioManager.STREAM_RING);
    if (ringvolume > 0) {
     mResumeAfterCall = (() || mResumeAfterCall);
     try {
      ();
     } catch (RemoteException e) {
      // TODO Auto-generated catch block
      ();
     }
    }
   } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
    // pause the music while a conversation is in progress
    mResumeAfterCall = (() || mResumeAfterCall);
    try {
     ();
    } catch (RemoteException e) {
     // TODO Auto-generated catch block
     ();
    }
   } else if (state == TelephonyManager.CALL_STATE_IDLE) {
    // start playing again
    if (mResumeAfterCall) {
     // resume playback only if music was playing
     // when the call was answered
     try {
      ();
     } catch (RemoteException e) {
      // TODO Auto-generated catch block
      ();
     }
     mResumeAfterCall = false;
    }
   }
  }
 };