Introduction to Android Lock Task Mode (Kiosk Mode)

Published on June 19, 2019

 

In this post we have a brief introduction of Android Lock Task Mode. When the device is in this mode, it can be locked to a single app or a set of apps.

Overview

Android can run tasks in an immersive, kiosk-like fashion called lock task mode. You might use lock task mode if you’re developing a kiosk application or a launcher to present a collection of apps. When the system runs in lock task mode, device users typically can’t see notifications, access non-whitelisted apps, or return to the home screen (unless the home screen is whitelisted).

Whitelist Apps

A DPC must whitelist apps before they can be used in lock task mode. Call DevicePolicyManager.setLockTaskPackages() to whitelist apps for lock task mode as shown in the following sample:

KOTLIN:

// Whitelist two apps.
private val KIOSK_PACKAGE = "com.example.kiosk"
private val PLAYER_PACKAGE = "com.example.player"
private val APP_PACKAGES = arrayOf(KIOSK_PACKAGE, PLAYER_PACKAGE)
// ...
val context = context
val dpm = context.getSystemService(Context.DEVICE_POLICY_SERVICE)
        as DevicePolicyManager
val adminName = getComponentName(context)
dpm.setLockTaskPackages(adminName, APP_PACKAGES)

JAVA:

// Whitelist two apps.
private static final String KIOSK_PACKAGE = "com.example.kiosk";
private static final String PLAYER_PACKAGE = "com.example.player";
private static final String[] APP_PACKAGES = {KIOSK_PACKAGE, PLAYER_PACKAGE};
// ...
Context context = getContext();
DevicePolicyManager dpm =
    (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName adminName = getComponentName(context);
dpm.setLockTaskPackages(adminName, APP_PACKAGES);

To find out the apps previously whitelisted for lock task mode, a DPC can call DevicePolicyManager.getLockTaskPackages(). Other apps can call DevicePolicyManager.isLockTaskPermitted() to confirm that an app package supports lock task mode.

Start Lock Task Mode

In Android 9.0 (API level 28) or higher, you can start another app’s activity in lock task mode. If an activity is already running in the foreground or background, you need to relaunch the activity. Call ActivityOptions.setLockTaskEnabled() and supply these options when starting the activity. The following snippet shows one way you can do this:

KOTLIN:

// Set an option to turn on lock task mode when starting the activity.
val options = ActivityOptions.makeBasic()
options.setLockTaskEnabled(true)
// Start our kiosk app's main activity with our lock task mode option.
val packageManager = context.packageManager
val launchIntent = packageManager.getLaunchIntentForPackage(KIOSK_PACKAGE)
if (launchIntent != null) {
    context.startActivity(launchIntent, options.toBundle())
}

JAVA:

// Set an option to turn on lock task mode when starting the activity.
ActivityOptions options = ActivityOptions.makeBasic();
options.setLockTaskEnabled(true);
// Start our kiosk app's main activity with our lock task mode option.
PackageManager packageManager = context.getPackageManager();
Intent launchIntent = packageManager.getLaunchIntentForPackage(KIOSK_PACKAGE);
if (launchIntent != null) {
  context.startActivity(launchIntent, options.toBundle());
}

In Android versions before 9.0, an app starts its own activities in lock task mode by calling Activity.startLockTask(). To call this method, the activity must be running in the foreground (see Activity-lifecycle concepts) so we suggest calling in the onResume() method of an Activity or Fragment. Here’s how you can call startLockTask():

KOTLIN:

// In our Fragment subclass.
override fun onResume() {
    super.onResume()
    // First, confirm that this package is whitelisted to run in lock task mode.
    if (dpm.isLockTaskPermitted(context.packageName)) {
        activity.startLockTask()
    } else {
        // Because the package isn't whitelisted, calling startLockTask() here
        // would put the activity into screen pinning mode.
    }
}

JAVA:

// In our Fragment subclass.
@Override
public void onResume() {
  super.onResume();
  // First, confirm that this package is whitelisted to run in lock task mode.
  if (dpm.isLockTaskPermitted(context.getPackageName())) {
    getActivity().startLockTask();
  } else {
    // Because the package isn't whitelisted, calling startLockTask() here
    // would put the activity into screen pinning mode.
  }
}

Don't start lock task mode when the device is locked because the user might not be able to unlock the device. You can call KeyguardManager methods to find out if the device is locked and use an Activity lifecycle callback (such as onResume() that's called after unlocking) to start lock task mode.

An app in lock task mode can start new activities as long as the activity doesn’t start a new task—except tasks that launch a whitelisted app. To understand how tasks relate to activities, read the guide Understand Tasks and Back Stack.

Alternatively, you can declare in your app manifest file how an activity should behave when the system is running in lock task mode. To have the system automatically run your activity in lock task mode, set the android:lockTaskMode attribute to if_whitelisted as shown in the following example:

<activity
    android:name=".MainActivity"
    android:lockTaskMode="if_whitelisted">
    <!-- ... -->
</activity>

You can learn more about declaring options in the app manifest file, by reading the lockTaskMode reference.

For more info on how to stop lock task mode and other stuff, please refer to the references.

References

https://developer.android.com/work/dpc/dedicated-devices/lock-task-mode

https://developer.android.com/work/dpc/dedicated-devices