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

6

61

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

Caste answered 29/1, 2010 at 13:27 Comment(6)
Yea i agree with your comments, But my requirement is geniune, as the application has some default things to do, which I say smart sharing my phone , like I am handovering my phone my child it should be able do whatever there in my application on only one click the application should not be terminated without my permission u shud provide the authetcation to terminate the application.Caste
A good example is the Todler Lock.Samuelsamuela
@Jay Askren, Yea its a good example, Do you have any Idea about the code hint for that.Caste
Unfortunately I don't. Sorry.Samuelsamuela
@JohnFeminella: well you're thinking about Android on personal devices, but what about an enterprise device you don't want the users wandering on the options but just use one application?Gastro
I thinks you can find your answer here https://mcmap.net/q/83463/-how-to-disable-home-and-other-system-buttons-in-androidSusie
M
29

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.

Maidservant answered 16/4, 2010 at 1:19 Comment(1)
Sorry to comment on this old one, but just to correct some confusion -- you cannot receive startActivity() Intents via a broadcast receiver. However, another activity can declare itself to be a HOME activity, which will then allow the user to choose a home screen implementation when they press the HOME button.Longmire
U
18

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

What does this 4 line java code means in android application?

Unstrained answered 16/6, 2011 at 5:19 Comment(5)
While this works to disable the home button, it brings up the lockscreen on top of my activity in my case. I am trying to make a lockscreen replacement, hence my need to disable home buttonPsychobiology
Interesting @Psychobiology what phone are you using, on a droid incredible, aria. Also what OS, 2.2 hereRundell
I tested on my Thunderbolt, which is rooted running CM7 (2.3.4).Psychobiology
Worked on my Acer Tablet running 3.1Commendatory
Fortunately, this security flaw has been fixed in newer versions of Android.Longmire
C
11

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.

Cade answered 21/6, 2011 at 15:11 Comment(2)
I gave +1 because it prevents exit, but it asked me to choose if my app was the default Launcher?? Acer Tablet 3.1Commendatory
This is a bad solution and it just tags your app as being a launcher. People may mistakenly switch their default launcher with your app. You don't want that.Burrow
S
5

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 your 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 always stay in the same screen.

Surgy answered 27/12, 2010 at 11:50 Comment(4)
I know this is a really old question but why would this be relevant to a runtime environment issue? I am not sure you can programatically disable the launcher (surprised if you could) or even should.Rundell
This answer does not seem to help, sorryCommendatory
Actually, it works but it need system privilege. Rather than adb to disable default launcher. It can also be done on code .Mastigophoran
This cmd 'pm disable com.android.launcher2' is error: Error: java.lang.IllegalArgumentException: Unknown package: com.android.launcher .Space
F
5

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.

Fidgety answered 30/4, 2012 at 8:22 Comment(3)
"The user must select the default home manager when it finish this application." - Not very useful if all you want is to disable the buttons only for your own app without messing up the launchers.Tenon
It would be preferable to just past the relevant code into the answer rather than have people open the zip and search for the code.Turbo
irrelavent answer.. this app messes up the home launcher buttonNanci
M
2

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.

Mok answered 5/9, 2011 at 23:52 Comment(2)
Hi, this seems usefull to my question. Can you help me with that #10801183 ?Midriff
This works with one side effect. My activity is suppose to take up full screen. Using your code above reverts it back to normal screen size, exposing Android's status bar, thereby opening up a security hole as any user can possibly get to some other part of the app via the status bar.Tenon

© 2022 - 2024 — McMap. All rights reserved.