0

I have my custom application running on the phone and it should run 24/7, so there is one requirement to restrict launching of home screen of android phone when user clicks on Home button of the phone means if my application running on the phone and user clicks on the Home button It should not navigate to the Home screen of the phone. It should always display my custom application on the phone.

So can anyone help me to achieve this functionality? Please share some example code so it would be helpful for me to implement.

Regards, Piks

piks
  • 1,621
  • 8
  • 32
  • 59
  • 1
    simply i believe this can't be done – Andro Selva Oct 24 '11 at 06:04
  • 1
    possible duplicate of [Is it possible to create an android app to make the phone run in sort of a kiosk mode?](http://stackoverflow.com/questions/5881373/is-it-possible-to-create-an-android-app-to-make-the-phone-run-in-sort-of-a-kiosk) – CommonsWare Oct 24 '11 at 06:39

4 Answers4

2

We cannot override home button functionality. Home button is reserved for OS

Sandeep Kumar P K
  • 7,412
  • 6
  • 36
  • 40
2

If you REALLY wanted to do this, you could make a custom launcher (home screen). Some tips for getting started are outlined here, but it's not a short and simple tweak.

It would also require that your users set their launcher to your custom launcher, and then make it their default, etc. This would really only be reasonable if you have 100% configuration control over the device.

If you did go this route, however, you'd be able to handle every case of home being pressed as well as have an easy hook for starting your 24/7 app.

Community
  • 1
  • 1
ImR
  • 787
  • 6
  • 8
1

try this:

@Override
public void onAttachedToWindow() {
    // TODO Auto-generated method stub
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);     
    super.onAttachedToWindow();
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_BACK:
            return true;
        case KeyEvent.KEYCODE_HOME:
            return true;
        }
    } else if (event.getAction() == KeyEvent.ACTION_UP) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_BACK:
            if (!event.isCanceled()) {
                // Do BACK behavior.
            }
            return true;
        case KeyEvent.KEYCODE_HOME:
            if (!event.isCanceled()) {
                // Do HOME behavior.
            }
            return true;
        default:
            return true;


        }
    }

    return super.dispatchKeyEvent(event);
}
Permita
  • 5,503
  • 1
  • 16
  • 21
  • Looks pretty good, but doesn't cover some strange cases, like holding the power button to bring up the shutdown popup and then hitting home. You could override the power button too, but then you'd have to pull the battery every time you wanted to shut off the device. – ImR Oct 24 '11 at 06:22
0

If you want to lock the home button. you can do this with the help of following code. but it will work for single activity only.

@Override

  public void onAttachedToWindow()

    {  
           this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);     
           super.onAttachedToWindow();  
    }
Dharmendra Barad
  • 949
  • 6
  • 14