Android信息亭模式-允许退出 [英] Android Kiosk Mode - Allow Exit

查看:127
本文介绍了Android信息亭模式-允许退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为信息亭模式编写Android应用程序.我正在使用本教程创建信息亭模式:

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/

但是,在本教程中,用户仍然可以单击主页,然后在2秒钟后返回应用程序.

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>

一切正常.但是,当用户尝试退出时(即用户以管理员身份登录),我的应用程序又回来了.我怀疑是因为我将其设置为"HOME".

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

推荐答案

您已安装了多个HOME屏幕(设备制造商和您的应用提供了默认屏幕).用户必须选择将您的应用程序设置为默认的HOME屏幕(通常在启动时发生).您现在要执行的操作是删除此首选"设置,以便用户可以选择其他默认" HOME屏幕(即:制造商的应用程序).这样做:

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");

,然后 finish()您的 MainActivity .

编辑:替代解决方案

作为一种替代解决方案,当您要退出"应用程序时,只需启动默认的HOME屏幕即可.为此,您需要知道默认HOME屏幕的程序包和类名称并对其进行硬编码,或者可以使用 PackageManager 这样的信息来扫描该信息:

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();

在这种情况下,您的应用程序仍设置为默认的HOME屏幕,因此,如果用户再次按HOME键,则将启动您的应用程序.但是用户随后可以退出"您的应用以再次返回到原​​始HOME屏幕.

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.

这篇关于Android信息亭模式-允许退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆