通过adb shell am start将数据发送回启动活动的脚本 [英] Send data back to the script which started the activity via adb shell am start
问题描述
我想从 adb
安装诊断应用程序,并从bash脚本中从中获取数据。我知道如何从 adb
开始活动,但是除非找到打印到 logcat 的方法,否则我找不到任何方法来获取数据。 code>并解析输出,但这听起来像是hack。有没有办法从使用
adb
开始的活动中接收数据?
I want to install a diagnostic app from adb
and get back data from it from within a bash script. I know how to start an activity from adb
, but I can't find any way to get data back unless maybe if I print to logcat
and parse the output, but that sounds like a hack. Is there a way to receive data back from an activity started using adb
?
推荐答案
如果可以将要发送回自动化脚本的数据序列化为长度小于4k的字符串-使用 logcat
是自然的选择。
只需执行以下操作即可使用 Log.i( UNIQUE_TAG,the_data_string_you_want_to_send_back_to_your_script);
将数据打印到日志中,然后在自动化脚本中使用以下命令捕获输出:
If the data that you want to send back to your automation script could be serialized into a string less than 4k long - using logcat
is a natural choice.
Just make your activity to print the data to the log with Log.i("UNIQUE_TAG", the_data_string_you_want_to_send_back_to_your_script);
and then use the following commands in your automation script to capture the output:
# clear the logcat buffer
adb logcat -c
# start your activity
adb shell am start <INTENT>
# this line will block until a string with "UNIQUE_TAG" tag and "Info" priority
# is printed to the main log
adb shell 'logcat -b main -v raw -s UNIQUE_TAG:I | (read -n 1 && kill -2 $((BASHPID-1)))'
# now you can capture the data and process it
DATA=$(adb logcat -d -b main -v raw -s UNIQUE_TAG:I)
在较新的Android版本中(7.0 +),其中 logcat
正确支持 -m< count>
, -t< time> ;
和 -T< time>
参数,您可以使用这个简单得多的版本,而不必使用 logcat清除日志-c
首先:
In more recent Android versions (7.0+) where logcat
properly supports -m <count>
, -t <time>
and -T <time>
parameters you can use this much simpler version without having to clear the log with logcat -c
first:
# instead of clearing the log just get the current timestamp
TS=$(adb shell 'echo $EPOCHREALTIME; log ""')
# start your activity
adb shell am start <INTENT>
# this command will return immediately if the data has been printed already or block if not
DATA=$(adb shell "logcat -b main -T $TS -m 1 -v raw -s UNIQUE_TAG:I")
这篇关于通过adb shell am start将数据发送回启动活动的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!