简体   繁体   English

Android Kiosk 模式 - 允许退出

[英]Android Kiosk Mode - Allow Exit

I am writing Android application for kiosk mode.我正在为 kiosk 模式编写 Android 应用程序。 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/我正在使用本教程创建信息亭模式: 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.但是,在教程中,用户仍然可以单击主页,然后在 2 秒后返回应用程序。

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.我怀疑是因为我将其设置为 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)?如果没有,是否有更好的方法来解决这个主页问题(即禁用主页按钮而不是实际将其设置为主页)?

You have multiple HOME screens installed (the default one provided by the device manufacturer, and your app).您安装了多个 HOME 屏幕(设备制造商和您的应用程序提供的默认屏幕)。 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 .然后finish()你的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扫描该信息,如下所示:

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.在这种情况下,您的应用程序仍设置为默认的 HOME 屏幕,因此如果用户再次按下 HOME 键,您的应用程序将被启动。 But the user can then "exit" your app to return again to the original HOME screen.但是用户可以“退出”您的应用程序以再次返回到原​​始主页屏幕。

You can use the device owner capabilities introduced in Android 5.0 to fully manage an Android device and use it as a kiosk.您可以使用 Android 5.0 中引入的设备所有者功能来全面管理 Android 设备并将其用作信息亭。 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 .设置设备所有者信息亭的最简单方法是使用Android 管理 API并配置信息亭策略

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM