我通过shell脚本打开一个带有节点的android模拟器:
var process = require('child_process');
process.exec('~/Library/Android/sdk/tools/emulator -avd Nexus_5_API_21_x86', processed);
function processed(data){
console.log('processed called', data, data.toString());
}
我需要能够检测到模拟器何时完成加载,因此我可以启动屏幕解锁,然后将浏览器启动到指定的网址(~/Library/Android/sdk/platform-tools/adb shell input keyevent 82
和~/Library/Android/sdk/platform-tools/adb shell am start -a android.intent.action.VIEW -d http://www.stackoverflow.com
)
然而,当我启动模拟器时,我似乎没有得到任何回复,并且该过程与模拟器保持联系。当关闭进程(ctrl + c)时,仿真器会随之关闭。 (这与在终端中直接运行shell命令的行为相同)
是否可以知道模拟器何时打开并加载?
如何在流程继续时执行其他命令 运行
答案 0 :(得分:1)
我像老板一样解决了它。
如果bootanimation停止,我能够设置一个计时器来检查一次。如果有,我们知道模拟器是打开并启动的。
var process = require('child_process');
process.exec('~/Library/Android/sdk/tools/emulator -avd Nexus_5_API_21_x86');
function isEmulatorBooted(){
process.exec('~/Library/Android/sdk/platform-tools/adb shell getprop init.svc.bootanim', function(error, stdout, stderr){
if (stdout.toString().indexOf("stopped")>-1){
clearInterval(bootChecker);
emulatorIsBooted();
} else {
console.log('we are still loading');
}
});
}
function emulatorIsBooted(){
//unlock the device
process.exec('~/Library/Android/sdk/platform-tools/adb shell input keyevent 82');
//gotourl
process.exec('~/Library/Android/sdk/platform-tools/adb shell am start -a android.intent.action.VIEW -d http://192.168.10.126:9876/');
}
bootChecker = setInterval(function(){
isEmulatorBooted();
},1000);
答案 1 :(得分:0)
万一其他人正在寻找它,请制作Fraser脚本的替代版本-该脚本还将在后台进程中启动实际的模拟器。
#!/usr/bin/env node
const process = require('child_process');
/* Get last emulator in list of emulators */
process.exec("emulator -list-avds|sed '$!d'", (_, stdout) => {
console.log('[android emulator] Booting')
/* Start emulator */
process.exec(`nohup emulator -avd ${stdout.replace('\n', '')} >/dev/null 2>&1 &`)
/* Wait for emulator to boot */
const waitUntilBootedThen = completion => {
process.exec('adb shell getprop init.svc.bootanim', (_, stdout) => {
if (stdout.replace('\n', '') !== 'stopped') {
setTimeout(() => waitUntilBootedThen(completion), 250)
} else {
completion()
}
})
}
/* When emulator is booted up */
waitUntilBootedThen(() => {
console.log('[android emulator] Done')
})
})