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

6
  • 4
    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. Commented Jan 30, 2010 at 4:41
  • 1
    A good example is the Todler Lock.
    – Jay Askren
    Commented Jan 30, 2010 at 22:23
  • @Jay Askren, Yea its a good example, Do you have any Idea about the code hint for that. Commented Feb 1, 2010 at 4:49
  • Unfortunately I don't. Sorry.
    – Jay Askren
    Commented Feb 1, 2010 at 5:29
  • 2
    @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?
    – m0skit0
    Commented Sep 5, 2012 at 14:12

6 Answers 6

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.

1
  • 6
    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. Commented Jun 28, 2011 at 14:30
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?

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 button
    – Josh
    Commented Jul 13, 2011 at 15:12
  • Interesting @Josh what phone are you using, on a droid incredible, aria. Also what OS, 2.2 here
    – Idistic
    Commented Jul 13, 2011 at 18:10
  • I tested on my Thunderbolt, which is rooted running CM7 (2.3.4).
    – Josh
    Commented Jul 14, 2011 at 3:06
  • Worked on my Acer Tablet running 3.1
    – shanabus
    Commented Sep 9, 2011 at 2:33
  • 6
    Fortunately, this security flaw has been fixed in newer versions of Android. Commented Oct 13, 2012 at 17:49
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.

2
  • 2
    I gave +1 because it prevents exit, but it asked me to choose if my app was the default Launcher?? Acer Tablet 3.1
    – shanabus
    Commented Sep 9, 2011 at 2:35
  • 1
    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.
    – zeh
    Commented Aug 8, 2013 at 14:25
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.

3
  • 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.
    – Johann
    Commented Aug 25, 2013 at 6:49
  • 6
    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.
    – Chloe
    Commented Sep 10, 2013 at 19:04
  • irrelavent answer.. this app messes up the home launcher button Commented Nov 15, 2013 at 11:04
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.

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.
    – Idistic
    Commented Jul 13, 2011 at 17:57
  • This answer does not seem to help, sorry
    – shanabus
    Commented Sep 9, 2011 at 2:31
  • Actually, it works but it need system privilege. Rather than adb to disable default launcher. It can also be done on code .
    – Yeung
    Commented May 30, 2014 at 5:48
  • This cmd 'pm disable com.android.launcher2' is error: Error: java.lang.IllegalArgumentException: Unknown package: com.android.launcher .
    – AmyNguyen
    Commented Feb 17, 2020 at 9:45
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.

2
  • Hi, this seems usefull to my question. Can you help me with that stackoverflow.com/questions/10800683/… ? Commented Jun 6, 2012 at 10:38
  • 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.
    – Johann
    Commented Aug 25, 2013 at 7:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.