如何在 Android 中以编程方式解锁屏幕?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1959012/
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-08-20 03:56:13  来源:igfitidea点击:

How can I unlock the screen programmatically in Android?

androidautomation

提问by SingleShot

I am working on a remote automated test framework for Android based on JUnit (tests run outside android, interacting with code inside it). It's all working fairly well, but one issue I have is that when I automatically start a fresh emulator, the screen starts out locked. This appears to affect my tests being able to run, plus, I want to watch the tests run (buttons clicked, text typed, etc.). If I manually start an emulator and unlock its screen, all works well.

我正在研究基于 JUnit 的 Android 远程自动化测试框架(测试在 android 外部运行,与其中的代码交互)。一切都运行良好,但我遇到的一个问题是,当我自动启动一个新的模拟器时,屏幕开始被锁定。这似乎会影响我的测试能够运行,另外,我想观看测试运行(单击按钮、输入文本等)。如果我手动启动模拟器并解锁其屏幕,一切正常。

Is there a way to programmatically unlock the screen in Android? A Java API, a command line or shell command, etc. would all be fine. Barring that, perhaps there is a way to start an emulator unlocked?

有没有办法在Android中以编程方式解锁屏幕?Java API、命令行或 shell 命令等都可以。除此之外,也许有一种方法可以启动解锁的模拟器?

采纳答案by Dave Webb

You can interact with the emulator via its console interface.

您可以通过其控制台界面与模拟器交互。

If you ever wondered why your emulator started with a number like 5554 - that's because that's the port the emulator listening on.

如果您想知道为什么您的模拟器以 5554 之类的数字开头 - 那是因为这是模拟器监听的端口。

You can find the port for running emulators with the adb devicescommand. It will have output like this:

您可以使用adb devices命令找到运行模拟器的端口。它将有这样的输出:

C:\>adb devices
List of devices attached
emulator-5554   device

So you can connect to the emulator using a command like:

因此,您可以使用以下命令连接到模拟器:

telnet localhost 5554

If you connect successfully you'll get an OKprompt and you can start entering commands.

如果连接成功,您将收到OK提示,您可以开始输入命令。

There are various commands but the one we are interested in is eventto simulate hardware events. We can unlock the screen by pressing Menuwhich we emulate with the following command:

有各种命令,但我们感兴趣的是event模拟硬件事件。我们可以通过按下Menu我们使用以下命令模拟的来解锁屏幕:

event send EV_KEY:KEY_MENU:1 EV_KEY:KEY_MENU:0

The EV_KEY:KEY_MENU:1is key-down event and the EV_KEY:KEY_MENU:0is the corresponding key-up event. Make sure you do both or the Menu key will be stuck down.

EV_KEY:KEY_MENU:1是钥匙的事件和EV_KEY:KEY_MENU:0是相应的键起事件。确保两者都执行,否则菜单键将被卡住。

I realise scripting this will be far from easy, but it's all I can think of to solve your problem.

我意识到编写脚本远非易事,但我能想到的就是解决您的问题。

Edit:I don't think event send EV_KEY:KEY_MENU:1 EV_KEY:KEY_MENU:0is emulating Menubut if I run the command just after I've started the emulator it doesunlock it. Not sure why but I guess this is a start.

编辑:我不认为event send EV_KEY:KEY_MENU:1 EV_KEY:KEY_MENU:0是在模拟,Menu但如果我在启动模拟器后立即运行命令,它确实会解锁它。不知道为什么,但我想这是一个开始。

回答by Drew Stephens

The one-liner that works for me on Android SDK r18 and r20 talking to a 4.0.3 (API 15) emulator:

在 Android SDK r18 和 r20 上与 4.0.3 (API 15) 模拟器对话的单行程序:

adb shell input keyevent 82

回答by luisr

I believe the following link has the answer you are looking for.

我相信以下链接有您正在寻找的答案。

http://developer.android.com/guide/topics/testing/activity_testing.html#UnlockDevice

http://developer.android.com/guide/topics/testing/activity_testing.html#UnlockDevice

Unlocking the emulator or device

解锁模拟器或设备

You may find that UI tests don't work if the emulator's or device's home screen is disabled with the keyguard pattern. This is because the application under test can't receive key events sent by sendKeys(). The best way to avoid this is to start your emulator or device first and then disable the keyguard for the home screen.

如果模拟器或设备的主屏幕被键盘保护模式禁用,您可能会发现 UI 测试不起作用。这是因为被测应用程序无法接收sendKeys() 发送的按键事件。避免这种情况的最佳方法是先启动模拟器或设备,然后禁用主屏幕的键盘锁。

You can also explicitly disable the keyguard. To do this, you need to add a permission in the manifest file (AndroidManifest.xml) and then disable the keyguard in your application under test. Note, though, that you either have to remove this before you publish your application, or you have to disable it with code in the published application.

您还可以明确禁用键盘保护。为此,您需要在清单文件 (AndroidManifest.xml) 中添加权限,然后在被测应用程序中禁用键盘保护。但请注意,您要么必须在发布应用程序之前将其删除,要么必须使用已发布应用程序中的代码将其禁用。

To add the the permission, add the element as a child of the element. To disable the KeyGuard, add the following code to the onCreate() method of activities you intend to test:

要添加权限,请将元素添加为元素的子元素。要禁用 KeyGuard,请将以下代码添加到您要测试的活动的 onCreate() 方法中:

mKeyGuardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE); mLock = mKeyGuardManager.newKeyguardLock("activity_classname"); mLock.disableKeyguard();where activity_classname is the class name of the activity.

mKeyGuardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE); mLock = mKeyGuardManager.newKeyguardLock("activity_classname"); mLock.disableKeyguard(); 其中activity_classname 是活动的类名。

回答by Yimin Li

Try this script:

试试这个脚本:

echo "event send EV_KEY:KEY_SOFT1:1" | nc -q1 localhost 5554
sleep 0.1
echo "event send EV_KEY:KEY_SOFT1:0" | nc -q1 localhost 5554
sleep 0.1
echo "event send EV_KEY:KEY_SOFT1:1" | nc -q1 localhost 5554
sleep 0.1
echo "event send EV_KEY:KEY_SOFT1:0" | nc -q1 localhost 5554
sleep 0.1

回答by chinnychin19

The easiest way to unlock the screen (assuming no password) is to pass a KEYCODE_MENU event; however, if the screen is already unlocked, this will actually open a menu, which probably isn't the functionality you're looking for.

解锁屏幕的最简单方法(假设没有密码)是传递一个 KEYCODE_MENU 事件;但是,如果屏幕已经解锁,这实际上会打开一个菜单,这可能不是您正在寻找的功能。

If you want a simple way to guarantee that the screen will be unlocked without opening a menu by accident, you should pass in a KEYCODE_POWER event first. This will guarantee that the device is in a locked state, so the menu event will only unlock the screen.

如果你想要一个简单的方法来保证屏幕会被解锁而不会意外打开菜单,你应该先传入一个 KEYCODE_POWER 事件。这将保证设备处于锁定状态,因此菜单事件只会解锁屏幕。

adb shell input keyevent KEYCODE_POWER; adb shell input keyevent KEYCODE_MENU

(Note: The 82 that appears in other responses is the KEYCODE_MENU constant.)

(注意:其他响应中出现的 82 是 KEYCODE_MENU 常量。)

回答by Vladimir Perkovi?

I managed to do it using the shell:

我设法使用外壳来做到这一点:

adb shell
#input keyevent 82

回答by Ganesh Devaraj

You can lock your Emulator Device by running this command from command prompt

您可以通过从命令提示符运行此命令来锁定您的模拟器设备

adb shell input keyevent 26

You can unlock your Emulator Device by running this command from command prompt

您可以通过从命令提示符运行此命令来解锁模拟器设备

adb shell input keyevent 82

After running the unlock command, you might have to wait for some minutes. Above unlock command will work, when you do not have any security lock.

运行解锁命令后,您可能需要等待几分钟。当您没有任何安全锁时,上述解锁命令将起作用。