love light
love light

Reputation: 123

how to disable home button in android?

i want to disable android device home button when my app runs.

i have tried following code but it din't help :

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_HOME)) {
            Toast.makeText(this, "You pressed the home button!",
                    Toast.LENGTH_LONG).show();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

     @Override
     protected void onNewIntent(Intent intent) {
     super.onNewIntent(intent);
     if (Intent.ACTION_MAIN.equals(intent.getAction())) {
     Log.i("MyLauncher", "onNewIntent: HOME Key");

     }
     }

Upvotes: 7

Views: 51925

Answers (4)

droidd
droidd

Reputation: 1371

I think this can be done in another style if suits your requirement. By creating your activity as home activity. If you want to disable home button and show your custom application activity as launcher when home button is pressed. Just add these lines in manifest for that activity for which you want your launcher.

<activity
            android:name="com.example.TempActivity"
            android:clearTaskOnLaunch="true"
            android:excludeFromRecents="true"
            android:launchMode="singleTask"
            android:screenOrientation="landscape"
            android:stateNotNeeded="true" >
             <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>
</activity>

After adding and run your application. if user presses the home button, Android will ask for which launcher you want your home. Then your have to select your application launcher ALWAYS not ONLY ONCE.

if you want fully disable the user, so that he cant move to another screen then set theme to fullscreen with NoTitlebar.

Upvotes: 16

Snehal Masne
Snehal Masne

Reputation: 3429

You can use below method to disable the Home key in android:

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

Upvotes: 1

shaobin0604
shaobin0604

Reputation: 1219

You can use Android-HomeKey-Locker to disable HOME KEY and other system keys(such as BACK KEY and MENU KEY)

NOTE

It seems that the solution only works on devices with hard home key

Hope this will help you in your application. Thanks.

Upvotes: 8

Raghunandan
Raghunandan

Reputation: 133560

You cannot disable home button.

There is no way to intercept the home button on Android, unless you make your app the home screen. This is for security reasons, so that malicious apps cannot take over your device by overriding all the buttons that can exit.

Home button is one sure short way to navigate to home screen.

If you want to handle the HOME button, implement a home screen.

Not able disable Home button on specific android devices

Check the answer by commonsware

Upvotes: 2

Related Questions