How do I prevent the user from closing my app?
Asked Answered
C

2

14

I've an android application that will be used in a restaurant, so I want that users can't exit from the app. The only thing that users can, is using application.

(If possible only admin can exit from app, by logging in or restarting device, I don't know which is the best way).

Is there a solution or other way to do this?

Chromatophore answered 7/4, 2013 at 11:44 Comment(6)
I can't think of any scenario in which user should not in be control of his device or the app. Also in extreme cases user can just power off device (pull battery) and restart the device without again restarting the said app.Heterochromatic
What you are looking for is a Kiosk mode app, I remember reading that on 4.2 it is possible to write home screen application that in behaviour will work like that: commonsware.com/blog/2013/02/20/android-4p2-for-kiosk-apps.htmlPrecession
@MasterChief he wanna use the application inside the restaurant where he doesn't want his workers to play angry birds using a freaking new android phone while working !. got it ?Rascally
@StyleMe You got it! ;) Not for workers but for clients.. Clients can see the restaurant's menu on tablet; the device isn't their smartphone/tablet, but it's mineChromatophore
@StyleMe Thank you! I'll try it.. but with this code how can I exit from app?Chromatophore
you can use double clicks on the back button or any method u like , just call finish();Rascally
K
3

Android only provide back button override function. It will not allow user to disable home key and recently open key.

But we can replace the home screen to our application in this way first we select our application as a launcher. So, if user press home key the home screen replace to our application and it will only open our application. For this effect add these lines in Manifest file:

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.HOME" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Koumis answered 20/2, 2023 at 17:27 Comment(1)
This is incredible!!!Beverleebeverley
R
2

you can override the onBackPressed method

@Override 
public void onBackPressed(){  
  Toast.MakeText(getApplicationContext(),"You Are Not Allowed to Exit the App", Toast.LENGTH_SHORT).show();
}

this will prevent the back button from exiting the application.

and then you will need to override the home button as well like

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_HOME) {
        Log.i("TEST", "Home Button");  // here you'll have to do something to prevent the button to go to the home screen 
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

EDIT: for new devices with android version 4.0.xx you'll have to override the recent apps button as well hope that helps you.

Rascally answered 7/4, 2013 at 12:22 Comment(2)
Does your "home button overrid" code belong in the app or in HomeScreen app?Bidle
App, this code posted last year so i cant guarantee it works now.Rascally

© 2022 - 2024 — McMap. All rights reserved.