0

I am working on an alarm feature for my application where, at a certain time, an alarm Activity is started, which subsequently starts either a vibration or a ringtone. The problem I've run into is that the vibration or ringtone is not stopped if the user presses the Home button.

I have overriden onKeyDown so that the alarm is stopped when a button is pressed, but that does not intercept the Home button code:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (mVibrator != null)
        mVibrator.cancel();
    if (mPlayer != null) {
        mPlayer.stop();
        mPlayer.release();
    }
    return super.onKeyDown(keyCode, event);
}

How can I go about stopping the vibration/ringtone when this occurs?

Tyler Treat
  • 14,640
  • 15
  • 80
  • 115
  • Check http://stackoverflow.com/questions/4783960/call-method-when-home-button-pressed-on-android and http://stackoverflow.com/questions/2079691/overriding-the-home-button-how-do-i-get-rid-of-the-choice – jalopaba Nov 08 '11 at 00:45

3 Answers3

1

I'd suggest stopping the alarm in onPause(). That gets called whenever another application comes to the foreground (including the home screen).

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

you can't intercept the home button - it's there for the reason that any app should allow the user to press home to immediately go to the home screen. you should consider stopping the alarm in your onStop() if you want the same effect.

josephus
  • 8,284
  • 1
  • 37
  • 57
  • or as per Ted Hopp, onPause works too. http://developer.android.com/reference/android/app/Activity.html\ – josephus Nov 08 '11 at 00:44
-1
  1. the Home key event is intercepted by the framework and is not passed to the application layer,so you can't monitor this event.
  2. my solution is that you can start a dummy activity (no status bar, no title bar, with a transparent background, so it looks like that there is no activity running).
  3. within that activity , override the onPause() method to stop ringing or vibration. so when user press the home key , it would be called. Just my idea. possibly there are better ideas.
Huang
  • 4,812
  • 3
  • 21
  • 20