35

I'd like to lock the screen. I want to disable the home key and only use the back key. How do I accomplish this?

Swati Garg
  • 995
  • 1
  • 10
  • 21
pengwang
  • 19,536
  • 34
  • 119
  • 168
  • 2
    see here, the fast answer it seems you need to implement a home screen replacement: http://stackoverflow.com/questions/3953689/how-to-lock-android-buttons-phone-from-code-screen-lock http://stackoverflow.com/questions/2162182/android-is-it-possible-to-disable-the-click-of-home-button/3955217#3955217 http://stackoverflow.com/questions/3724509/going-to-home-screen-programmatically http://www.toddlerlock.com/3.html – mishkin Oct 17 '10 at 21:17
  • Hi.. Have you got the solution. i'm also trying to disable Home key for my lock screen app, but its not working. – Vaishali Sharma Aug 10 '16 at 05:11

5 Answers5

30

Use this method to disable the Home key in android

@Override
public void onAttachedToWindow() {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();
}
ASH
  • 2,748
  • 2
  • 28
  • 29
  • 5
    Does this work with a fullscreen SurfaceView and MediaPlayer? Seems like it does not. Oh I got it: in this case TYPE_KEYGUARD_DIALOG works. – Yar Mar 26 '12 at 13:24
  • The user will still be able to slide down the Notification Bar... and once the bar is down they can click home and it will work. – MindWire Apr 20 '12 at 17:43
  • Have you tried it? I dont think so that the notification bar would work. – ASH Apr 24 '12 at 05:21
  • 2
    The notification bar still shows up and after bringing the notification bar down, pressing the home key goes back to the home screen. Any work arounds? Theme.Black.NoTitleBar and requestWindowFeature(Window.FEATURE_NO_TITLE) doesn't work. – Ragunath Jawahar Oct 19 '12 at 11:43
  • I am also looking for a solution to this issue: my fullscreen activity still exits when pressing the home button, even if I call setType with keyguard. Should I use an API level higher than Android 2.3? – ravemir Jul 07 '13 at 17:30
  • I tried TYPE_KEYGUARD_DIALOG with SurfaceView and it works greatly – Mohammed Subhi Sheikh Quroush Jul 11 '13 at 10:24
  • Back key is blocked by this but not the home key. – Naresh Sharma Jul 18 '14 at 05:28
  • 7
    its throws exception of illegalstate exception – Anand Savjani Nov 25 '15 at 05:48
  • 7
    This doesn't seem to work on modern Android versions. It throws an exception that one can't change the type of the window after it had been added. – Grishka Nov 21 '16 at 06:38
23

I found a way to tackle the 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.

Vlad Schnakovszki
  • 8,434
  • 6
  • 80
  • 114
amiekuser
  • 1,630
  • 1
  • 19
  • 31
  • 1
    pm disable com.android.launcher2 should be pm disable com.android.launcher – kakopappa May 12 '11 at 06:42
  • @ kakopappa the package launcher is no longer used. The package is launcher2. Check the code in Git – amiekuser May 31 '11 at 09:25
  • I was try like this but i did't get exactly how to do this? will you please give me source code for more detail. thanks. – Datta Kunde Jun 08 '11 at 09:45
  • Hi I just want to disable the home key and not the back key. how this can be acheived? – Aada Nov 22 '12 at 10:31
  • I have added above attributes to my activity in manifest file but still the home button takes my to launcher home screen even though I have set my app as default home screen. What should I do? – Umer Farooq Aug 23 '13 at 07:12
  • Thanks man saved my day how to uninstall the app :) just disable these filters: – Muhammad Oct 05 '15 at 16:18
13

This solution works up to 3.x only.

Okay, this was supposed to be a hard question. But here is a way to crack it.

Override the below method in your Activity,

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

And now handle the key event like this,

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
   if(keyCode == KeyEvent.KEYCODE_HOME)
    {
     Log.i("Home Button","Clicked");
    }
   if(keyCode==KeyEvent.KEYCODE_BACK)
   {
        finish();
   }
 return false;
}
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
9

Add this code to your MainActivity class:

Timer timer;
MyTimerTask myTimerTask;

@Override
protected void onResume() {
    super.onResume();

    if (timer != null) {
        timer.cancel();
        timer = null;
    }
}

@Override
protected void onPause()
{
    if (timer == null) {
        myTimerTask = new MyTimerTask();
        timer = new Timer();
        timer.schedule(myTimerTask, 100, 100);
    }

    super.onPause();
}

private void bringApplicationToFront()
{
    KeyguardManager myKeyManager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
    if( myKeyManager.inKeyguardRestrictedInputMode())
        return;

    Log.d("TAG", "====Bringging Application to Front====");

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    try
    {
        pendingIntent.send();
    }
    catch (PendingIntent.CanceledException e)
    {
        e.printStackTrace();
    }
}

public void onBackPressed() {
    // do not call super onBackPressed.
}

class MyTimerTask extends TimerTask {
    @Override
    public void run() {
        bringApplicationToFront();
    }
}

It's not a locking for 'home' button, but user can't switch to another app for a long time (more than 100 milliseconds), maybe this is what you want.

Stas BZ
  • 1,184
  • 1
  • 17
  • 36
1

To disable home button, try this:

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

Problem with notification bar pulled down can be solved by hiding notification bar like here:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.xxxx);
    getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN);
    ....
}

or set fullscreen theme for your Activity or Application in manifest:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
Alex
  • 688
  • 6
  • 8