Android Kiosk Mode - Allow Exit
Asked Answered
S

2

9

I am writing Android application for kiosk mode. I am using this tutorial to create kiosk mode: http://www.andreas-schrade.de/2015/02/16/android-tutorial-how-to-create-a-kiosk-mode-in-android/

However, in the tutorial, the user still can click on home and then the application back after 2 seconds.

So, I did a bit of modification to disable the home button by making my application as a home. I did it by put this in my manifest:

<activity android:name=".MainActivity"
          android:launchMode="singleInstance">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.HOME"/>
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Everything work well. But when the user try to exit (ie. user login as administrator), my application is back again. I suspect because I set it as HOME.

My question is, how to allow my app to Exit. Is it possible to go back to actual home when my app exit? If not, is there a better way to tackle this home problem (ie. disable home button and not actually set it as home)?

Smuggle answered 7/9, 2017 at 12:50 Comment(1)
There is a "real" Kiosk mode in Android that you can trigger as a device owner. This is a really big topic, you can read more about it if you're interested here: developer.android.com/work/cosu.htmlOmnipotent
W
11

You have multiple HOME screens installed (the default one provided by the device manufacturer, and your app). The user must have chosen that your app should be the default HOME screen (this usually happens at boot time). What you now want to do is to remove this "preferred" setting so that the user can choose a different "default" HOME screen (ie: the manufacturer's app). Do that like this:

PackageManager pm = getPackageManager();
pm.clearPackagePreferredActivities ("your.package.name");

and then finish() your MainActivity.


EDIT: Alternative solution

As an alternative solution, when you want to "exit" your app, you just launch the default HOME screen instead. To do this, you need to either know the package and class name of the default HOME screen and hardcode that, or you can scan for that info using PackageManager like this:

PackageManager pm = getPackageManager();
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
List<ResolveInfo> infoList = pm.queryIntentActivities(homeIntent, PackageManager.MATCH_DEFAULT_ONLY);
// Scan the list to find the first match that isn't my own app
for (ResolveInfo info : infoList) {
    if (!"my.package.name".equals(info.activityInfo.packageName)) {
        // This is the first match that isn't my package, so copy the
        //  package and class names into to the HOME Intent
        homeIntent.setClassName(info.activityInfo.packageName,
                       info.activityInfo.name);
        break;
    }
}
// Launch the default HOME screen
startActivity(homeIntent);
finish();

In this case, your app is still set as the default HOME screen, so if the user presses the HOME key again, your app will be started. But the user can then "exit" your app to return again to the original HOME screen.

Woermer answered 15/9, 2017 at 13:56 Comment(3)
This is exactly what I am looking for! But 1 problem (may be an easy fix), how do set my application back as a home for next launch? Because when I do pm.clearPackagePreferredActivities, the my app is no longer a Home and the user now can use the "Home" button. I still need to prevent the user from tapping on the "Home" button (ie. "Home" button is doing nothing when user press it)Smuggle
I imagine you need to tell the user to reboot. On boot the user should be asked to choose a HOME screen.Woermer
I've added an alternative solution you can try. Maybe this is a better match to your requirements.Woermer
T
3

You can use the device owner capabilities introduced in Android 5.0 to fully manage an Android device and use it as a kiosk. Among other things this allows you to prevent the user from exiting the app by tapping the home button.

The simplest way to set up a device owner kiosk is to use the Android Management API and configure a kiosk policy.

Trix answered 13/9, 2017 at 22:24 Comment(9)
Hi Fred, the problem I have is that I can't shut down or exit from the current app. I need to enable "Administrator" to shutdown (or exit) from the app. This shutdown (or exit from the app) must only be available for "Administrator" which they will login via my app.Smuggle
If you use the Android Management API you could change the policy to disable kiosk mode when the administrator logs in your app, allowing to exit the app, and then change the policy again to re-enable kiosk mode once the administrator is done.Trix
Hi Fred, I don't think it is practical in my case. So, basically our app is used in the warehouse. At the end of the day, the lady who worked in admin will collect all tablets and used her barcode tag to login. Then she will get bunch of menu buttons and one of them is "Exit" button. I've been looking how to change the policy programmatically with no success. So, unless I can change the policy programmatically, I can't use the Android Management API :(Smuggle
You can change the policy programmatically with the devices.patch method, that you can call with any service account that has access to your Cloud project (see Create a service account). When the admin logs in on the tablet you call devices.patch to set the "non-kiosk" policy, which immediately exits the kiosk mode. You can also remotely unlock multiple tablets at a time if you want.Trix
Hi Fred, this is really COOL!!! Thanks! I will investigate more. Just one quick question, those warehouse tablets will not have access to the internet. Their WIFI is isolated and only allowed to be connected to the back server. Is there a way to disable/enable this policy without an internet connection? And I was told at least one warehouse has absolutely no internet connection :(Smuggle
Hi Fred, just another quick question that you might know the answer. Is it possible, with this Android Management API), to "shut down" (turn off) the tablet programmatically? In that case, I don't need to change the policy?Smuggle
The Android Management API is a Cloud based API and therefore requires internet connection to change policies. And I don't think there is a turn off feature, there is only wipe and lock.Trix
Hi Fred, I have investigate this Android Management API. It is very promising. But I can't apply it on my case as it required internet and our warehouse may or may not have internet. I've voted +1 for sharing this info. Thank you so much!Smuggle
0 i am trying to understand this Android Entreprise process , i looks interesting but kind of complicated , so i wonder if it's possible to manage a set of devices localy using kind of local console (web) to upload , install and update apps and configure kiosk mode and so on.the main goal is to use Android Management API without going through google online management system.Embalm

© 2022 - 2024 — McMap. All rights reserved.