-1

I am part of a team developing an android application and we are trialing it for one day soon.

I was wondering if there is a way to "lock" the users into the app, (most users aren't smartphone literate so don't want them hitting the home button by accident and then being confused about how to get back).

I know there is a reason this probably isn't possible (due to malware apps locking users out of their phone essentially) but is there any way to do this?

I've looked around on SO and found some discussions but couldn't find a concrete answer.

We have Nexus 4 phones running Jelly Bean 4.2.2

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
poncho
  • 1,100
  • 2
  • 15
  • 38
  • Have you tried overriding key press event of home button? – SKT Feb 25 '13 at 10:45
  • 2
    @SKT: not possible, it is never received by an app – RvdK Feb 25 '13 at 10:47
  • 2
    It might be somewhat achievable by creating a launcher application =/ but I think the answer is no unless you make your own Android build like cyanogenmod and put it on the phone. – Warpzit Feb 25 '13 at 10:51
  • Ah so could I put on cyanogenmod and that has some way of allowing me to disable home? That would potentially work if it is easy to do? – poncho Feb 25 '13 at 11:32

1 Answers1

0
@Override
public void onAttachedToWindow() {
    // TODO Auto-generated method stub

    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);

    super.onAttachedToWindow();
}

and override home key press event in on key down method.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub

    if(keyCode == KeyEvent.KEYCODE_HOME) {
        Toast.makeText(this, "Sorry", Toast.LENGTH_SHORT).show();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_BACK) {
        Toast.makeText(this, "Sorry No back press", Toast.LENGTH_SHORT).show();
        return true;
    } else {
        return super.onKeyDown(keyCode, event);
    }
}
SKT
  • 1,821
  • 1
  • 20
  • 32
  • Hi, thanks for the quick answer - I get the second part of the code but what does the first part do? – poncho Feb 25 '13 at 12:02
  • It keeps the screen to the current screen even any of the keys are pressed like home or back... – SKT Feb 25 '13 at 12:05
  • Ah okay that's awesome, I'll give that a try soon and then a tick if it works :) – poncho Feb 25 '13 at 14:35
  • Just a quick question, if I do this, how do I then get out of the app? Is there no way of now getting out other than a hard reset or building in a button that quits the application itself? – poncho Feb 25 '13 at 15:39
  • Hmm this didn't work for me - I got the error: java.lang.IllegalArgumentException: Window type can not be changed after the window is added. - I only have one Activity and this was added to it – poncho Feb 25 '13 at 15:45
  • The above code will not work. The OS does not send the KEYCODE_HOME on the onKeyDown event. – Hoan Nguyen Feb 25 '13 at 18:02
  • It was working in Ginger bread and the application do received KEYCODE_HOME event. I will check for a solution in jelly-bean – SKT Feb 26 '13 at 10:04
  • Yeah it didn't work but thanks for the help, I found a way of doing it mentioned above by registering the app as a launcher and then making it the default launcher. – poncho Feb 26 '13 at 14:13