2

I'm trying to make a lockscreen for android. Whenever I press the home button, it brings me back to the home screen. I tried various apps from google play. The Go Locker app does not let you to go to home screen unless you unlock the screen. I don't want my application to return to home screen unless I swipe the screen to unlock it. Is there anyway to do this functionality? It's been 3 days and I couldn't find anything about it.

Thanks a lot.

Sid18
  • 23
  • 1
  • 4

1 Answers1

3

Here is some code:

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

With this method, the HOME Button stops working in this activity (only this activity). Then you just re implement as it was a normal button event.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    boolean res = super.onKeyDown(keyCode, event);
    switch (keyCode) {
    case KeyEvent.KEYCODE_HOME: {
        // Your Home button logic;
        return false;
    }
    return res;
}

Warning: The onAttachedToWindow() override this actually works, but it could be pretty dangerous if all buttons are disabled, locking the user in, with the only way to exit the application being removing the battery, so code properly, handle all the cases.

OR

Came across this method of overriding the home button click, hope it works for you:

In AndroidManifest.xml

<activity
    ...
    android:launchMode="singleTask">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <category android:name="android.intent.category.HOME" />
        <category android:name="android.intent.category.DEFAULT" />
        ....
    </intent-filter>
</activity>

You need launchMode="singleTask" so the intent is delivered to the already running application instead of creating a new instance.

In the activity:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (Intent.ACTION_MAIN.equals(intent.getAction())) {
        //your code goes here.
    }
}

You do not get a key event.

Sagar Pilkhwal
  • 3,998
  • 2
  • 25
  • 77
  • I tried doing that but it doesn't work. It brings me back to home screen. – Sid18 Sep 05 '14 at 20:01
  • are you sure that this method is being called ? – Sagar Pilkhwal Sep 05 '14 at 20:08
  • i have ed the code, there are two ways mentioned, let me know if it works for you. – Sagar Pilkhwal Sep 05 '14 at 20:24
  • The second part works but it has problems. If I press home button when I'm not in my application( i.e anywhere in my android system but not the application) it returns back to the application. Anyways, thanks a lot for help. This code may come handy someday. – Sid18 Sep 05 '14 at 22:34
  • @Sid18 Did you resolve this issue. I am struggling for over an week to do this. But couldn't find anything concrete. Please advise if you resolved this issue. – Gaurav May 10 '15 at 10:44
  • @Gaurav have you solved the problem. I am seeking the solutions for kitkat device. – Ajay Oct 15 '15 at 09:40
  • @Ajay I found one solution wherein we can make the activity as launcher activity. In that case Android will prompt to set the launcher for his phone and user is expected to select our app as an launcher. This way we can disable home button in any activity for our app. I have another solution but that isn't working all the times. – Gaurav Oct 15 '15 at 09:50
  • @Gaurav i have added following code to manifest android:launchMode= "singleTask" .But still my application is not considered as a Launcher activity when i touch on home button... Any help/ tutorial is appreciated – Ajay Oct 15 '15 at 10:29
  • @Ajay Give me some time and I will get back with link of git where I found this. – Gaurav Oct 15 '15 at 10:50
  • @Ajay Check the link https://github.com/ASPOOK/Android-LockScreen/blob/master/README.md – Gaurav Oct 15 '15 at 14:09