5

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

3 Answers3

6

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

JaVadid
  • 7,107
  • 13
  • 42
  • 54
1

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!

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Hi CommonsWare, Thanks for your reply, however, I found the intent action using which I can be notified when the Alarm goes off. The intent actions string is "com.android.alarmclock.ALARM_ALERT". This works fine for me, however, I now need to handle the events generated by pressing the Snooze and Dismiss buttons in the Alarm clock. Any help on this would be useful. Thanks a lot, Asheesh – Asheesh Vashishtha Apr 27 '10 at 04:49
  • 1
    That `Intent` action is not part of the SDK -- nothing starting with "com.android" is part of the SDK. Do not rely upon that `Intent`. It may not be used on some devices (those that have replaced the standard Alarm Clock with a custom app). Also, it may not work on future versions of Android. http://www.androidguys.com/2009/12/14/code-pollution-reaching-past-the-sdk/ – CommonsWare Apr 27 '10 at 11:11
  • Thanks a lot, CommonsWare. So what you're suggesting is that if we don't use the Intent action, there's no other way of handling the events generated by the Alarm clock application? Also, there's no such Alarm clock that's provided out-of-the box by the SDK? Is it something that has been added on top of it later? Please correct me on this. – Asheesh Vashishtha Apr 28 '10 at 04:04
  • 2
    "Also, there's no such Alarm clock that's provided out-of-the box by the SDK?" The SDK is a programming interface. It does not include apps, any more than the Windows SDK includes Notepad or Minesweeper. – CommonsWare Apr 28 '10 at 10:45
1

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

welshk91
  • 1,613
  • 18
  • 16