piks
piks

Reputation: 1673

Restrict Homescreen launch when user clicks on Home button of android phone

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

Upvotes: 0

Views: 629

Answers (4)

Dharmendra Barad
Dharmendra Barad

Reputation: 949

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();  
    }

Upvotes: 0

ImR
ImR

Reputation: 887

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.

Upvotes: 2

Permita
Permita

Reputation: 5493

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);
}

Upvotes: 1

Sandeep Kumar P K
Sandeep Kumar P K

Reputation: 7482

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

Upvotes: 2

Related Questions