Starting from Android 5 (API 21) there is better support for kiosk applications.
Kiosk applications = applications that the user cannot exit from.
As you can see in this screenshot, the home button and recent apps button are missing. The top notifications drawer is also inaccessible.
This solution is good for devices that serve a specific purpose. For example point of sale solutions or tourist information stands.
Device Owner App
1. The first step is to create an app and provision it as the device owner app. Download the attached example, change its package name to any value you like and run it on the device in Release mode.
2. Connect the device to the PC in USB debug mode. Open command line and run the following command:
Replace your.package.name.here with the correct package name.
B4X:
adb shell dpm set-device-owner your.package.name.here/anywheresoftware.b4a.objects.AdminReceiver2
Existing user accounts on the device must first be removed (Settings - Accounts).
3. Add this code to the manifest editor (it is already there in the example project):
B4X:
AddApplicationText(<receiver android:name="anywheresoftware.b4a.objects.AdminReceiver2"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="@xml/device_admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>)
CreateResource(xml, device_admin.xml,
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<limit-password />
<reset-password />
<force-lock />
</uses-policies>
</device-admin>
)
4. See how the example code locks and unlocks the activity.
Note that for the solution to be more robust it is recommended to make the activity a home launcher activity.
This is done by adding this code to the manifest editor:
B4X:
AddActivityText(Main,
<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>
)
With this code you need to click on the home button once and then you will be asked to choose the home launcher app. Choose your app.
This is important to make sure that your app starts as soon as possible after a restart.
During development you might prefer to disable it.
It depends on Administrator library v1.1+: https://www.b4x.com/android/forum/threads/updates-to-internal-libraries.59340/#post-518017