cts测试源码中会发现都是如下格式的shell命令来实现的:
//灭屏 然后 亮屏
protected void gotoKeyguard() throws DeviceNotAvailableException {
sleepDevice();//灭屏
wakeUpDevice();//亮屏
}
protected void sleepDevice() throws DeviceNotAvailableException {
int retriesLeft = 5;
runCommandAndPrintOutput("input keyevent 26");
do {
if (isDisplayOn()) {
log("***Waiting for display to turn off...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
log(e.toString());
// Well I guess we are not waiting...
}
} else {
break;
}
} while (retriesLeft-- > 0);
}
protected void wakeUpDevice() throws DeviceNotAvailableException {
runCommandAndPrintOutput("input keyevent 224");
}
protected String runCommandAndPrintOutput(String command) throws DeviceNotAvailableException {
final String output = executeShellCommand(command);
log(output);
return output;
}
protected String executeShellCommand(String command) throws DeviceNotAvailableException {
return executeShellCommand(mDevice, command);
}
protected static String executeShellCommand(ITestDevice device, String command)
throws DeviceNotAvailableException {
log("adb shell " + command);
return device.executeShellCommand(command);
}
以上命令行相当于shell命令:
adb shell command
即:
adb shell input keyevent 26 //灭屏
adb shell input keyevent 224 //亮屏
类似的还有如下命令行:
adb shell input keyevent 3 //HOME
adb shell input keyevent 4 //BACK
adb shell input keyevent 82 //MENU
adb shell am stack list //列出stack中的activity,显示的activity在最上面
am start -a android.intent.action.MAIN -c a