Android Kiosk Mode - Allow Exit

人盡茶涼 提交于 2019-12-05 00:11:27

问题


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)?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/46096849/android-kiosk-mode-allow-exit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!