6

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

     }
     }
love light
  • 113
  • 1
  • 1
  • 5

4 Answers4

15

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.

mehrdadjg
  • 384
  • 1
  • 11
droidd
  • 1,361
  • 10
  • 15
  • ... and than your app gets started everytime the users presses the home button ... usually not what you want to archive – derHugo Jun 11 '18 at 07:18
8

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.

shaobin0604
  • 1,179
  • 12
  • 14
  • @menu_on_top clone the repo, add HomeLockerLib to your app project's dependancies – shaobin0604 Mar 25 '14 at 02:57
  • Hey, thank you very much for your solution, but is it possible for soft home keys also? Please suggest some idea if it is possible. – Prince Apr 22 '15 at 14:25
  • I installed a "Button Locker" app from the Playstore. I don't know if it is the same app you are talking about. It works. It locks the defective home button of my phone. But the app is useless because it has a side effect which makes it unusable. It disables the softkeyboard which does not appear anymore when I want to type in Whatsapp. – Elmue Jun 09 '20 at 22:39
2

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

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • :D I am asking about the question (too many questions are there). Not saying that your answer is duplicate :) – Pankaj Kumar Jun 19 '13 at 06:08
  • duplicate of question already exist on SO with same topic. Don't increase you number of answers with some links copied from another answer suggested for same topic on SO. – Manish Dubey Jun 19 '13 at 06:09
  • @Raghunandan : You should give comment with hyperlink on Commonsware answer instead of post new answer. – Manish Dubey Jun 19 '13 at 06:14
1

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();
}
Snehal Masne
  • 3,403
  • 3
  • 31
  • 51