java 如何从代码(屏幕锁定)锁定安卓按钮/手机?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3953689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:06:57  来源:igfitidea点击:

How to lock android buttons/phone from code (screen lock)?

javaandroidlocking

提问by Martin

Possible Duplicate:
Lock the android device programatically

可能的重复:
以编程方式锁定 android 设备

I have made a simple Android app with a pin-code/screen lock. The user have to write a password to lock the phone and then repeat it to unlock the phone. The problem is that the user still can press back, home (etc) to exit the app without writeing the code. How can I prevent this?

我制作了一个带有密码/屏幕锁定的简单 Android 应用程序。用户必须写一个密码来锁定手机,然后重复密码来解锁手机。问题是用户仍然可以按返回、主页(等)退出应用程序,而无需编写代码。我怎样才能防止这种情况?

回答by Aaron C

You can block the back button by overwriting dispatchKeyEvent()in your Activityclass, and returning true if event.getKeyCode()is equal to KeyEvent.KEYCODE_BACK. But you cannot block the Home button from going to the home page.

您可以通过dispatchKeyEvent()Activity类中覆盖来阻止后退按钮,如果event.getKeyCode()等于则返回 true KeyEvent.KEYCODE_BACK。但是您不能阻止主页按钮进入主页。

I have heard of some trickery where you register your application as a receiver of the android.intent.category.HOMEintent. This would cause the Android OS to load your activity if the user presses the home button. If you can get this to work, you could then load the "actual" home screen if the user has entered the correct password. This approach is likely to behave differently on different devices and Android versions, however, and it probably would do nothing to stop the Hold-Home task list from appearing.

我听说过将应用程序注册为android.intent.category.HOME意图接收者的一些技巧。如果用户按下主页按钮,这将导致 Android 操作系统加载您的活动。如果你可以让它工作,如果用户输入了正确的密码,你就可以加载“实际”主屏幕。然而,这种方法在不同的设备和 Android 版本上的行为可能会有所不同,并且它可能无法阻止 Hold-Home 任务列表的出现。

The bottom line is the Android OS has been designed to prevent just the thing you are trying to do: an application should not be able to take control over the phone and prevent other applications (especially the Phone) from running.

最重要的是,Android 操作系统旨在防止您尝试做的事情:应用程序不应能够控制手机并阻止其他应用程序(尤其是手机)运行。

回答by Tyler Treat

It's my understanding that you cannot block the home key as it's a security feature that allows the user to always exit an app. Pressing home will not close the activity however.

我的理解是您不能阻止主页键,因为它是一项允许用户始终退出应用程序的安全功能。但是,按回家不会关闭活动。

回答by mishkin

the guys who created free "Toddler lock" apk somehow managed to did it - download and see for yourself :)

创建免费的“幼儿锁”apk 的人以某种方式设法做到了 - 下载并亲自看看:)

when you run it, it asks for permission to run as home app and you need to check the box "use as default". To exit from app, you need to press on all 4 corners of the screen. All the buttons but power one are locked. So there is a way for sure to do that!

当您运行它时,它会要求允许作为家庭应用程序运行,您需要选中“默认使用”框。要退出应用程序,您需要按屏幕的所有 4 个角。除电源按钮外,所有按钮均已锁定。所以肯定有办法做到这一点!

he explains to users how it works here: http://www.toddlerlock.com/3.html

他在这里向用户解释它是如何工作的:http: //www.toddlerlock.com/3.html