0

I have a method that stop the my app ringing tone.
CommonUtils.pauseAlarm();
and i want to apply these methods on Device home button click but it does not get home button click event.

All key event detect like menu,up, down button click but not get home button click

if ((keyCode == KeyEvent.KEYCODE_HOME)) {          
    CommonUtils.pauseAlarm();// background services 
    return true;
}

I have search many sites, some sites give the solution like

@Override
protected void onUserLeaveHint() {
    Log.d("onUserLeaveHint", "Home button pressed");
    CommonUtils.pauseMusic();
    super.onUserLeaveHint();
}

Also i am try this function

@Override
public void onPause(){
    super.onPause();
    CommonUtils.pauseMusic();
}

but those method also call on my app another button click event So please give me any idea? What i am doing wrong.

Abhishek
  • 1,654
  • 2
  • 18
  • 31
Ujjwal Jha
  • 115
  • 1
  • 8

1 Answers1

1

Android has stopped users from modifying the action that the Home Button has.

As stated by silvia-aut (answer)

On older Android version this is working. But Android changed this, because they say "Home Button should stay Home Button" and they don't want that anybody override the Home Button. And because of this reason your code is not working anymore.

If you want to do something when the home button is pressed, then do this in the onPause method.

Secondly, try to first call your method, then pause:

@Override
public void onPause(){
    CommonUtils.pauseMusic();
    super.onPause();
}
Community
  • 1
  • 1
TejjD
  • 2,571
  • 1
  • 17
  • 37
  • i am also try in this way but same problem occurs , onPause() method also call when i open another activity and my alarm is stop. – Ujjwal Jha Jan 25 '16 at 06:05
  • Do not call onPause everywhere. Put your method call in the base activity that you are extending, then in your other activities just override onPause() and call super – TejjD Jan 25 '16 at 06:07