Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle Alarm notification in Android?

Tags:

android

alarm

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 snoozed or dismissed, I'll then resume the playback.

I googled a lot for the Alarm handling, but what I found was the way to enable Alarm notifications through code, set the intent and then handle it. However, no where could I locate just handling the Alarm notification part. I don't need to set the Alarm on, it could've been set by the user, and I don't need to programmatically. All I need is just handle that notification.

Any ideas on this would be extremely useful?

Thanks, Asheesh

like image 667
Asheesh Vashishtha Avatar asked Apr 26 '10 12:04

Asheesh Vashishtha


3 Answers

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...

like image 157
JaVadid Avatar answered Sep 23 '22 03:09

JaVadid


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!

like image 25
CommonsWare Avatar answered Sep 19 '22 03:09

CommonsWare


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).

like image 37
welshk91 Avatar answered Sep 23 '22 03:09

welshk91