Hi Habr. Many of us work every day on certain applications, create something of our own or fulfill the requirements of a negligent customer. One of these requirements could be something like: “I want to prevent the exit from the application, I don’t want to be able to do anything else, except being in my applications”. In this topic I want to share my thoughts on how to do this.
The first thing we come to is that we learn about a new type of application - Kiosk Mode - a special type of application, usually running on devices in public places. In such applications, the functionality to which the user can have access is limited to the application itself. Access to the system or any settings is not allowed. From such applications can not exit. And since we are talking about Android, then, unfortunately, their API does not provide any options for creating this type of application. Let us try to figure out what we can do with all this and how to please our customer at least a little.
Hard buttons and connectors
Each device provides us with a specific set of buttons and various different connectors, such as USB, power, and so on. In this case, as developers, we can do nothing special. When placing it, it is necessary to provide a structure that would completely block the possibility of using such things.
')
Panel with virtual buttons
Starting with Android 3.0, we are offered some alternative to hardkey buttons, such as a panel at the bottom of the screen. This includes the buttons "Back", "Home", "Options", the status of the battery and so on.

But for our application, this panel can be very disturbing, as it allows you to exit the application, enter the system settings, etc., which can disrupt the necessary work flow. But there is one way to fade this panel. All that will be described hereinafter, requires root rights on your device.
And so, we will need to execute a simple command:
service call activity 79 s16 com.android.systemui
This command can be executed either via adb or directly from the application:
Runtime.getRuntime().exec(new String[]{"su","-c","service call activity 79 s16 com.android.systemui"});
This will force a hostile panel for us to leave our eyes. But if suddenly we need this panel, we can also return it simply with the command:
am startservice -n com.android.systemui / .SystemUIService
Or:
Runtime.getRuntime().exec(new String[]{"am","startservice","-n","com.android.systemui/.SystemUIService"});
This solution worked successfully on Android 3.0. How are things with 4.0 so far I can not say.
Home and Back buttons
If for some reason the panel is needed, but you need to change the behavior of the buttons, then here is one recipe. Let's start with the simple, Back button. Everything is easy here, we redefine the method:
@Override public void onBackPressed() {
Now more difficult, the Home button. Google prudently reacted to this button, since this is the only way to leave the application and return to the main screen, but for us it’s a disaster, that’s what we don’t need. What we can do:
- We need to add the necessary settings for our starting activation to AndroidManifest:
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter>
This will give us the following; when you click on Home, a dialog will pop up:

Already not bad, but how to make that this dialogue did not appear, and our application opened.
- We can select the checkbox, restart the device and our application will be launched by default, but this is not a problem, we can also easily remove this checkbox in the system settings. Let's go the other way, we will find the launcher in the depths of the operating system and simply rename it and / or send it to another place:
mv / path / Laucher.apk / path 2 / LaucherOld.apk
Everything, on it the main application on the device one - ours. No more pop-up dialogs. If you need to have access to Launcher, then either return it back, or create a secret menu and launch Laucnher from where we moved it.
Problems
Unfortunately, not everything is as rosy as it seems. Not all issues are resolved to the end. For example, look at the standard keyboard:

In the lower left corner there is a small button that allows you to go to the keyboard settings, which is not good:

The solution is to create your own keyboard, the benefit of the API allows it. But the decision is too dreary, for the sake of one small button.
Various system dialogs
While our application is running, various system alerts and notifications may pop up, such as low battery power or system update notifications. Some of them may have access to system settings.


Unfortunately, I was not able to figure out how to prohibit the display of such dialogues. If the first dialog can be turned off in the settings, then with the second one is worse, from it we can directly get into the settings. The solution is to do the same as we did with Launcher.apk - to transfer or rename the settings application.
Conclusion
In this article I tried to describe methods known to me for creating Kiosk mode applications. If habrasoobshchestva have any other methods, it would be great to share them. Thank.