Arquimedes Ribeiro
Arquimedes Ribeiro

Reputation: 21

Block the home button on android 4

I need to lock the home button in an app, because it will be used by older people and they will not know how to get back if they accidently touch on the home button. I already have that code below but that is not working on android 4.

What I really want is when somebody touches the home button, it does not do anything. Do you have any idea that can help me?

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

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR
        && (keyCode == KeyEvent.KEYCODE_BACK    || keyCode == KeyEvent.KEYCODE_HOME)
    && event.getRepeatCount() == 0) 
    {
        onBackPressed();
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public void onBackPressed() {
    // Do nothing
    return;
}

Upvotes: 2

Views: 382

Answers (1)

Raghav Sood
Raghav Sood

Reputation: 82533

The only way to do this is to make your app the launcher app on that device, which may not be desirable to the vast majority of Android users, regardless of their age.

Upvotes: 3

Related Questions