This article describes a list of powerful methods to implement a Kiosk Mode in Android. A strong>Kiosk Mode is useful if you develop an app and want prevent any other applications to run in the foreground.
Note: You are looking for a solution to restrict the user access to your website(s) on Android? I just published an App for that purpose. You can find it here.
A few things to consider
Exit mechanism: Don't forget to implement and test an exit mechanism in your app.
Talk to your users: Be careful, if you want to distribute your app through the Play Store. Tell your users how they can leave your app before they enter the Kiosk Mode.
Nothing is completely secure: There are techies like you out there who can bypass your restrictions if the device is not physically secured.
A Kiosk Mode is implemented by disabling various Android features that can be used to leave your app.
The following features are affected:
The back button
The home button
The recent apps button
The power button
The volume buttons
First of all we need to make sure your Kiosk app starts automatically after booting your device.
Add the following permission as a child of the manifest element to your Android manifest:
Now, your app has the permission to receive the RECEIVE_BOOT_COMPLETED broadcast. This message means that the phone was booted.
At next, add an intent filter to the manifest:
Create a class called BootReceiver that extends BroadcastReceiver and add your code to the onReceive method to start your application:
Disable the back button
This is a simple task: Just override onBackPressed in your Activity class. Please note that you can not override onBackPressed inside fragments!
Disable the power button
Sadly, disabling the power button is not possible without custom modification to the core of the Android operating system. Nevertheless, it is possible to detect the result of the button press and react on it.
Short power button press:
You can detect a short button press by handling the ACTION_SCREEN_OFF intent and kick the screen back to life with acquiring a wake lock. What a hack!
Please note that you can't declare ACTION_SCREEN_OFF in the AndroidManifest.xml! You are only allowed to catch them while your application is running.
For that reason, create a class called OnScreenOffReceiver that extends BroadcastReceiver and add the following code:
Create a subclass of Application and add the following code:
Add the permission WAKE_LOCK to your manifest:
Register your subclass of Application in your manifest (android:name=".AppContext"):
To make the wake up a bit smarter, add the following line in your activity (before setContentView is called!). This line deactivates the lock screen:
Long power button press:
Now we come to my favorite hack: It is very simple but powerful.
Add the following snippet to your activity. It will surely prevent long press button.
The idea is simple: in case any system dialog pops up, we kill it instantly by firing an ACTION_CLOSE_SYSTEM_DIALOG broadcast.
Disable the volume button
If necessary, you can easily deactivate the volume buttons by consuming both button calls. Just override dispatchKeyEvent in your Activity and handle the volume buttons:
Disable the home button and detect when new applications are opened
Since Android 4 there is no effective method to deactivate the home button. That is the reason why we need another little hack. In general the idea is to detect when a new application is in foreground and restart your activity immediately.
At first create a class called KioskService that extends Service and add the following snippet:
Add the following method in your AppContext class to start the service via application context creation.
Last, add the service declaration and the permission for retrieving the foreground process to the manifest:
Basically, the thread checks every two seconds if your application is running in foreground. If not, the thread will immediately recreate your activity and your Android Kiosk is visible again.
Prevent screen dimming
It is also very easy to keep the screen bright as long as your app is visible (also forever). You only have to add the following flag to your root layout:
Developing a kiosk-based application is not the easiest part in Android development, but it is definitely possible to create a "robust" Kiosk Mode. The only disadvantage is the long list of (very) dirty hacks.
Do you want an Android Kiosk App? Just contact me: blog //at// andreas-schrade.de - I am happy to hear from you.
Android and backend freelancer with over ten years of experience, passion for performant, robust and flexible enterprise applications. The 3 most important things I offer are quality, deadline and communication. I already worked for very big international companies. Please ask for references.
Do you need an individual Android App or Web Application?