用shell命令通过节点启动Android模拟器时的callback

我打开一个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()); } 

我需要能够检测到模拟器加载完成后,我可以启动一个屏幕解锁,然后启动浏览器到指定的URL( ~/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

然而,当我启动模拟器时,我似乎没有得到任何回报,并且该过程保持与模拟器相关联。 当closures进程(ctrl + c)时,仿真器随之closures。 (这与在terminal中直接运行shell命令的行为是一样的)

  1. 是否有可能知道模拟器何时打开和加载?

  2. 如何在进程继续运行时执行其他命令?

我像老板一样解决了它。

如果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);