Menu

Enable Kiosk Mode

Now that you have set up the device admin receiver, activated your license, and configured the required permissions, you can use the Knox APIs to enter Kiosk mode.

Enable/disable default Kiosk Mode

This method demonstrates how to toggle the default Kiosk Mode.

 private void toggleKiosk() {
        EnterpriseDeviceManager enterpriseDeviceManager =
                EnterpriseDeviceManager.getInstance(this.getApplicationContext());
        KioskMode kioskMode = enterpriseDeviceManager.getKioskMode();

        boolean isKioskModeEnabled = kioskMode.isKioskModeEnabled();

        try {
            // If in Kiosk Mode, disable Kiosk Mode
            // If not in Kiosk Mode, enable Kiosk Mode with the current package as the home package
            if (isKioskModeEnabled) {
                mUtils.log(getString(R.string.leaving_kiosk));
                removeShortcutFromKioskMode();
                kioskMode.disableKioskMode();
            } else {
                mUtils.log(getString(R.string.entering_kiosk));
                addShortcutToKioskMode();
                kioskMode.enableKioskMode();
            }
        } catch (SecurityException e) {
            mUtils.processException(e, TAG);
        }
    }

Enable/disable custom Kiosk Mode

This method demonstrates how to use custom Kiosk Mode.

 private void toggleCustomKiosk(KioskSetting kioskSetting, boolean isKioskModeEnabled) {
        EnterpriseDeviceManager enterpriseDeviceManager =
                EnterpriseDeviceManager.getInstance(this.getApplicationContext());
        KioskMode kioskMode = enterpriseDeviceManager.getKioskMode();

        try {
            // If in Kiosk Mode, disable Kiosk Mode
            // If not in Kiosk Mode, enable Kiosk Mode with the current package as the home package
            if (isKioskModeEnabled) {
                mUtils.log(getString(R.string.leaving_kiosk));
                removeShortcutFromKioskMode();
                kioskMode.disableKioskMode(kioskSetting);
            } else {
                mUtils.log(getString(R.string.entering_kiosk));
                addShortcutToKioskMode();
                kioskMode.enableKioskMode(kioskSetting);
            }
        } catch (SecurityException e) {
            mUtils.processException(e, TAG);
        }
    }

Custom kiosk mode then prompts the user to choose which restrictions to set for Kiosk Mode.

For example – the user can disable the option to change device settings and expand the status bar in Kiosk Mode.

 private void promptCustomKioskSetting() {

        // Build a dialog to show the user
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        EnterpriseDeviceManager enterpriseDeviceManager =
                EnterpriseDeviceManager.getInstance(this.getApplicationContext());
        KioskMode kioskMode = enterpriseDeviceManager.getKioskMode();

        final boolean isKioskModeEnabled = kioskMode.isKioskModeEnabled();

        if (isKioskModeEnabled) {
            builder.setTitle(getString(R.string.exit_kiosk_setting));
            View viewInflated = LayoutInflater.from(this).inflate(R.layout.exit_custom_kiosk,
                    (ViewGroup) findViewById(R.id.kiosk_config_group), false);

            builder.setView(viewInflated);
            builder.setPositiveButton(getString(R.string.option_confirm), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    KioskSetting kioskSetting = new KioskSetting();
                    kioskSetting.settingsChanges = true;
                    kioskSetting.statusBar = false;
                    kioskSetting.statusBarExpansion = true;
                    kioskSetting.systemBar = false;
                    kioskSetting.taskManager = true;
                    kioskSetting.homeKey = true;
                    kioskSetting.airCommand = true;
                    kioskSetting.airView = true;
                    kioskSetting.multiWindow = true;
                    kioskSetting.smartClip = true;
                    kioskSetting.navigationBar = false;
                    kioskSetting.wipeRecentTasks = false;
                    kioskSetting.clearAllNotifications = false;

                    toggleCustomKiosk(kioskSetting, isKioskModeEnabled);
                }
            });
        } else {
            builder.setTitle(getString(R.string.configure_kiosk_setting));
            View viewInflated = LayoutInflater.from(this).inflate(R.layout.prompt_kiosk_setting,
                    (ViewGroup) findViewById(R.id.kiosk_config_group), false);

            final CheckBox chkSettingsChanges = viewInflated.findViewById(R.id.chkSettingsChange);
            final CheckBox chkStatusBar = viewInflated.findViewById(R.id.chkAllowStatusBar);
            final CheckBox chkStatusBarExpansion = viewInflated.findViewById(R.id.chkStatusBarExpansion);
            final CheckBox chkSystemBar = viewInflated.findViewById(R.id.chkAllowSystemBar);
            final CheckBox chkTaskManager = viewInflated.findViewById(R.id.chkAllowTaskManager);
            final CheckBox chkHomekey = viewInflated.findViewById(R.id.chkAllowHomekey);
            final CheckBox chkAirCommand = viewInflated.findViewById(R.id.chkAllowAirCommand);
            final CheckBox chkAirView = viewInflated.findViewById(R.id.chkAllowAirview);
            final CheckBox chkMultiwindow = viewInflated.findViewById(R.id.chkAllowMultiwindow);
            final CheckBox chkSmartclip = viewInflated.findViewById(R.id.chkAllowSmartClip);
            final CheckBox chkNavBar = viewInflated.findViewById(R.id.chkAllowNavBar);
            final CheckBox chkWipeRecentTasks = viewInflated.findViewById(R.id.chkWipeRecentTasks);
            final CheckBox chkClearNotifications = viewInflated.findViewById(R.id.chkClearNotifications);

            builder.setView(viewInflated);
            builder.setPositiveButton(getString(R.string.option_confirm), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    KioskSetting kioskSetting = new KioskSetting();
                    kioskSetting.settingsChanges = chkSettingsChanges.isChecked();
                    kioskSetting.statusBar = chkStatusBar.isChecked();
                    kioskSetting.statusBarExpansion = chkStatusBarExpansion.isChecked();
                    kioskSetting.systemBar = chkSystemBar.isChecked();
                    kioskSetting.taskManager = chkTaskManager.isChecked();
                    kioskSetting.homeKey = chkHomekey.isChecked();
                    kioskSetting.airCommand = chkAirCommand.isChecked();
                    kioskSetting.airView = chkAirView.isChecked();
                    kioskSetting.multiWindow = chkMultiwindow.isChecked();
                    kioskSetting.smartClip = chkSmartclip.isChecked();
                    kioskSetting.navigationBar = chkNavBar.isChecked();
                    kioskSetting.wipeRecentTasks = chkWipeRecentTasks.isChecked();
                    kioskSetting.clearAllNotifications = chkClearNotifications.isChecked();

                    toggleCustomKiosk(kioskSetting, isKioskModeEnabled);
                }
            });
        }

        builder.setNegativeButton(getString(R.string.option_cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        builder.show();
    }

Add or remove an app shortcut to Kiosk Mode

Paste the following code in MainActivity() to add a shortcut to Kiosk mode.

 public void addShortcutToKioskMode() {
        EnterpriseDeviceManager enterpriseDeviceManager =
                EnterpriseDeviceManager.getInstance(this.getApplicationContext());
        ApplicationPolicy applicationPolicy = enterpriseDeviceManager.getApplicationPolicy();
        KioskMode kioskMode = enterpriseDeviceManager.getKioskMode();
        String kioskHomePackage = null;

        // Since Android O, the homePkgName parameter should be null and the shortcut is added
        // only in current launcher.
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            kioskHomePackage = kioskMode.getKioskHomePackage();
        }

        try {
            boolean result = applicationPolicy.addHomeShortcut(getPackageName(), kioskHomePackage);
            if (result) {
                mUtils.log(getString(R.string.added_app_to_kiosk));
            } else {
                mUtils.log(getString(R.string.failed_add_app_to_kiosk));
            }
        } catch (SecurityException e) {
            mUtils.processException(e, TAG);
        }
    }

Paste the following code in MainActivity() to remove the Kiosk mode shortcut.

 public void removeShortcutFromKioskMode() {
        EnterpriseDeviceManager enterpriseDeviceManager =
                EnterpriseDeviceManager.getInstance(this.getApplicationContext());
        ApplicationPolicy applicationPolicy = enterpriseDeviceManager.getApplicationPolicy();
        KioskMode kioskMode = enterpriseDeviceManager.getKioskMode();
        String kioskHomePackage = null;

        // Since Android O, the homePkgName parameter should be null and the shortcut is added
        // only in current launcher.
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            kioskHomePackage = kioskMode.getKioskHomePackage();
        }

        try {
            boolean result = applicationPolicy.deleteHomeShortcut(getPackageName(), kioskHomePackage);
            if (result) {
                mUtils.log(getString(R.string.removed_app_from_kiosk));
            } else {
                mUtils.log(getString(R.string.failed_remove_app_kiosk));
            }
        } catch (SecurityException e) {
            mUtils.processException(e, TAG);
        }
    }

Tutorial Progress

You are 6/7 done! Go to the next step.

Share it: