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

Janusz
  • 187,060
  • 113
  • 301
  • 369
Vinayak Bevinakatti
  • 40,205
  • 25
  • 108
  • 139
  • 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. – Vinayak Bevinakatti Jan 30 '10 at 04:41
  • 1
    A good example is the Todler Lock. – Jay Askren Jan 30 '10 at 22:23
  • @Jay Askren, Yea its a good example, Do you have any Idea about the code hint for that. – Vinayak Bevinakatti Feb 01 '10 at 04:49
  • Unfortunately I don't. Sorry. – Jay Askren Feb 01 '10 at 05: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 Sep 05 '12 at 14:12
  • I thinks you can find your answer here http://stackoverflow.com/a/23349558/2540947 – Lê Quang Duy Apr 28 '14 at 19:51

6 Answers6

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.

Yotam Sadan
  • 15
  • 1
  • 7
synic
  • 26,359
  • 20
  • 111
  • 149
  • 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. – CommonsWare Jun 28 '11 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?

Community
  • 1
  • 1
Jeffrey
  • 189
  • 1
  • 4
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.

rekire
  • 47,260
  • 30
  • 167
  • 264
Jean-François
  • 374
  • 3
  • 8
  • 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 Sep 09 '11 at 02: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 Aug 08 '13 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.

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
davide.gironi
  • 227
  • 2
  • 4
  • 7
  • 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 Aug 25 '13 at 06: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 Sep 10 '13 at 19:04
  • irrelavent answer.. this app messes up the home launcher button – Srikanth Pai Nov 15 '13 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.

halfer
  • 19,824
  • 17
  • 99
  • 186
amiekuser
  • 1,630
  • 1
  • 19
  • 31
  • 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 Jul 13 '11 at 17:57
  • This answer does not seem to help, sorry – shanabus Sep 09 '11 at 02:31
  • Actually, it works but it need system privilege. Rather than adb to disable default launcher. It can also be done on [code](http://developer.android.com/reference/android/content/pm/PackageManager.html#setApplicationEnabledSetting%28java.lang.String,%20int,%20int%29) . – Yeung May 30 '14 at 05:48
  • This cmd 'pm disable com.android.launcher2' is error: Error: java.lang.IllegalArgumentException: Unknown package: com.android.launcher . – AmyNguyen Feb 17 '20 at 09: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.

Richard Rout
  • 1,296
  • 1
  • 15
  • 28
  • Hi, this seems usefull to my question. Can you help me with that http://stackoverflow.com/questions/10800683/android-ics-native-lockscreen ? – Mario Lenci Jun 06 '12 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 Aug 25 '13 at 07:11