Search code examples
androidalarmmanagerandroid-alarmstelephonymanager

Which AudioFocus do I need to set for an alarm application


From this blog I learned that, for all media applications, I should request AudioFocus and start the playback only if the AudioFocus is granted.

am.requestAudioFocus(audioFocusChangeListener,AudioManager.STREAM_ALARM,AudioManager.AUDIOFOCUS_GAIN);

I am making an alarm application.
As an alarm, it should always ring until dismissed - except during phone calls (it is logical).

I can track if a call is ringing or idle by using the TelephonyManager

TelephonyManager telephonyManager =
        (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

    switch (telephonyManager.getCallState()) {
      case TelephonyManager.CALL_STATE_IDLE:
        outsidePause = false;
        break;
      default:
        outsidePause = true;
        break;
    }
PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override public void onCallStateChanged(int state, String incomingNumber) {
      switch (state) {

        case TelephonyManager.CALL_STATE_IDLE:
          outsidePause = false;
          Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE,
              Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
          android.provider.Settings.System.putInt(getContentResolver(),
              android.provider.Settings.System.SCREEN_BRIGHTNESS, (0));
          am.setStreamVolume(AudioManager.STREAM_ALARM, 0, 0);
          setInnerData();
          startAlarm();
          break;
        default:
          outsidePause = true;
          vibrator.cancel();
          pauseAlarm();
          break;
      }
      super.onCallStateChanged(state, incomingNumber);
    }
  };

Taking in account the TelephonyManager, is it useful and nessesary to use the AudioFocus (if it is, which mode is preferrable)?

Can another application take it from mine, making silencing the alarm?


Solution

  • Seems, that using Audio focus if ok. Othet application does not take it, untill I abandone it.