我想等到android手机启动并且MediaScanner完成。
之后我想使用 adb 执行一个操作。
adb wait-for-device 将在手机启动序列完成之前完成。
如何通过 ADB 捕获例如 BOOT_COMPLETE 广播?类似于:等待启动完成。我不知道这是否可能?
You can keep polling for sys.boot_completed
or dev.bootcomplete
system properties.
As for the code, I do not know what environment and/or scripting language you are using. It's pretty straightforward. First you need to find which property is being set to "1" up on boot completion by your phone's software. Let's say it is dev.bootcomplete
. Then the following command would return control back to your script after the phone is booted up
adb wait-for-device shell 'while [[ -z $(getprop dev.bootcomplete) ]] ; do sleep 1; done'
对于那些在 Windows 环境中工作的人来说,这个批处理脚本对我有用。
它一直等到 ADB 守护程序运行,然后开始轮询sys.boot_completed
属性并等待值 1。
它不像单行那么优雅,但我的 PATH 环境变量中列出了脚本,因此可以直接调用它。
adb wait-for-device
:CheckAgain
set value=
for /f "delims=" %%a in ('adb shell getprop sys.boot_completed') do @set value=%%a
IF NOT "%value%" == "1" (
timeout /t 2 /nobreak >NUL
goto CheckAgain
)
如果您的设备没有安装busybox(请参阅android 中的shell 脚本给出[: not found),您可以尝试在您的计算机中进行迭代。像这样的东西会起作用:
while [ `adb shell getprop dev.bootcomplete` -nq "1" ] ; do sleep 1; done
如果您的设备安装了busybox,您可以按照Alex P. 的评论继续:
adb shell 'while [ ""`getprop dev.bootcomplete` != "1" ] ; do sleep 1; done'
当然,语法取决于您的机器(POSIX 等)。