How to handle Alarm notification in Android?

前端 未结 3 1603
孤独总比滥情好
孤独总比滥情好 2020-12-10 08:28

I\'m developing an media player application for Android, for which I need to handle any Alarm notification, and based on that I\'ll pause my playback. When the Alarm in snoo

相关标签:
3条回答
  • 2020-12-10 09:20

    I ran into a similar situation while developing a media player. My solution was to use the AudioManager's OnAudioFocusChangeListener.

    You implement the listener in the class like so

    public class VideoPlayerHelper implements AudioManager.OnAudioFocusChangeListener {
    

    Then you override onAudioFocusChange

    @Override
    public void onAudioFocusChange(int focusChange) {
        switch (focusChange) {
    
            //Just fall through by omitting break
            case AudioManager.AUDIOFOCUS_LOSS:
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                LogUtil.log(LogUtil.DEBUG, TAG, "AUDIOFOCUS_LOSS or AUDIOFOCUS_LOSS_TRANSIENT"); //Custom logging class
                if (isPlaying()) {
                    pause();
                    mAudioManager.abandonAudioFocus(VideoPlayerHelper.this);
                }
                break;
            case AudioManager.AUDIOFOCUS_GAIN:
                LogUtil.log(LogUtil.DEBUG, TAG, "AUDIOFOCUS_GAIN"); //Custom logging class
                break;
            default:
                break;
        }
    }
    

    The key here is AudioManager.AUDIOFOCUS_LOSS_TRANSIENT. This was the code the listener kept receiving when the alarm clock would go off (on The Note 5). So I simply handled AudioManager.AUDIOFOCUS_LOSS_TRANSIENT the same as AudioManager.AUDIOFOCUS_LOSS by pausing the media player and letting go of the audio focus.

    When we setup the media player, I added this line before adding the data source

    player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    

    Make sure your code for starting the media player also has this line in it (I have it in the start code and onResume code in case the alarm went off while the app was in the background).

    mAudioManager.requestAudioFocus(VideoPlayerHelper.this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
    

    That line helps you get back the audio focus when you hit the play button after dismissing the alarm clock.

    You should also let go off audio focus when you're finished with the media player. I put this line of code in the onStop and onDetach methods.

    mAudioManager.abandonAudioFocus(VideoPlayerHelper.this);
    

    It's not as much setup as you may think and it allows you to adjust your media player whenever unexpected audio is introduced (such as an alarm clock or timer goes off).

    0 讨论(0)
  • 2020-12-10 09:27

    HI Asheesh Vashishtha,

    Correct me on this, but AFAIK whenever any other application even if it is the alarm clock, is activated, your activity will surely go in background. So i guess u can override the OnPause and OnResume functions to put your bit of code. As far as snooze or other things are concerned, they all will result in the Alarm Activity getting destroyed(or paused, don know much about it) and your activity will get resumed. So that wont be a matter of concern for u!

    Hope this helps...

    0 讨论(0)
  • 2020-12-10 09:30

    AFAIK, there is no way for you to be notified of what the Alarm Clock application does, any more than you get notified about any other third-party alarm clock.

    Note that AlarmManager -- what you were probably reading about -- is not the same as the Alarm Clock application.

    Sorry!

    0 讨论(0)
提交回复
热议问题