0

I want to handle device home button click in my android application.When googled it is said that this Link works.But I have some doubts.

  1. Is it supported by all android versions?If not which of them are supported?

  2. Is there any consequence exist because of using onAttachedToWindow() method?

  3. Is there any way to handle Home button click(Except this)?

Thanks in Advance

Devu Soman
  • 2,246
  • 13
  • 36
  • 57

3 Answers3

0

This only works in previous version. But from os version 4.0 it is not working (although in my emulators this doesnt work after api level 11 but I am pointing ics according to many other links).

stinepike
  • 54,068
  • 14
  • 92
  • 112
  • ,I want to make when user clicks on HOME button, the application goes to background and restarts automatically with in a specified time.How to do this? – Devu Soman Mar 21 '13 at 07:18
  • check this http://stackoverflow.com/a/10118640/931982 and this http://stackoverflow.com/a/5040120/931982 – stinepike Mar 21 '13 at 07:21
0

Found this on other posts.

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.

cnuis2cool
  • 131
  • 1
  • 5
-1

yeah its work please try this code

@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
}

And now handle the key event like this,

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

if(keyCode == KeyEvent.KEYCODE_HOME)
{
 Log.i("Home Button","Clicked");
}
if(keyCode==KeyEvent.KEYCODE_BACK)
{

    finish();
 }
   return false;
};