Android - Is It possible to disable the click of home button

AndroidAndroid Widget

Android Problem Overview


I have an application, when it launches I have to disable all the buttons on Android device, I succeeded in disabling end call and others. I need to disable home button click. It should not produce any action on click.

Any suggestions highly appreciated

Android Solutions


Solution 1 - Android

I'm pretty sure Toddler Lock just uses a BroadcastReciever and listens for Intent.ACTION_MAIN and the category Intent.CATEGORY_HOME - that's why when you first launch it, it tells you to check the "use this application as default" box, and makes you select toddler lock.

So, it's not really blocking the Home button at all, it's just setting itself up as the default broadcast receiver for:

Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);

When you launch Toddler Lock, it probably sets an internal flag, and if you press the home button, it just brings the window to the front. If the flag is not set, it probably launches Launcher explicitly.

I hope that makes sense. It's just a theory, but I'm almost 100% sure that's how it's done.

Solution 2 - Android

Add following code to your activity:

@override

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

Edit:

This works in all older version of android. But will not work in ICS and jelly bean and will give you crash in app

https://stackoverflow.com/questions/12560180/what-does-this-4-line-java-code-means-in-android-application

Solution 3 - Android

Add this in your manifest.xml for your main activity:

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

The HOME button will always (re-)launch your activity. Works in Froyo.

Solution 4 - Android

I found a way to tackle HOME key. For your application set the manifest as

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

Now ur application is an alternate Launcher application.

Use the adb, and disable the launcher application using package manager

pm disable com.android.launcher2

Now the Home key press will laways stay in the same screen.

Solution 5 - Android

here you can find my Android sample application which persist on the home page. Home, Back, Call, Power button are disabled. User can end the application only by typing a password.

Solution 6 - Android

A further addition to Jeffreys post, here is something that worked for me (and still allows translucent theme)

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

Becuase it makes the keyguard come up, you could also just disable the keyguard whilst the app is in use:

KeyguardManager keyguardManager = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();

This works really well for making your own keyguard app.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVinayak BevinakattiView Question on Stackoverflow
Solution 1 - AndroidsynicView Answer on Stackoverflow
Solution 2 - AndroidJeffreyView Answer on Stackoverflow
Solution 3 - AndroidJean-FrançoisView Answer on Stackoverflow
Solution 4 - AndroidamiekuserView Answer on Stackoverflow
Solution 5 - Androiddavide.gironiView Answer on Stackoverflow
Solution 6 - AndroidRichard RoutView Answer on Stackoverflow