【问题标题】:Send data back to the script which started the activity via adb shell am start将数据发送回通过 adb shell am start 启动活动的脚本
【发布时间】:2014-04-03 16:49:50
【问题描述】:

我想从adb 安装一个诊断应用程序,并从 bash 脚本中取回数据。我知道如何从adb 开始活动,但我找不到任何方法来取回数据,除非我打印到logcat 并解析输出,但这听起来像是一个黑客。有没有办法从使用adb 开始的活动接收数据?

【问题讨论】:

  • 打印到logcat然后解析结果不是hack
  • @AlexP。好的,在这种情况下,您或任何人可以告诉我如何打开 logcat 并等待结果吗?如果我在am start 之后立即运行logcat -d,则输出将不存在,我宁愿不猜测该过程需要多长时间才能开始。
  • 只用am start -W
  • @Paschalis,您有权发表自己的意见。但很可能你只是不知道如何正确地做到这一点。
  • @Paschalis,你做错了。如果操作正确,丢失任何数据的可能性为 0。

标签: android bash adb


【解决方案1】:

如果您想要发送回自动化脚本的数据可以序列化为长度小于 4k 的字符串 - 使用 logcat 是自然的选择。 只需使用Log.i("UNIQUE_TAG", the_data_string_you_want_to_send_back_to_your_script); 将您的活动打印到日志中,然后在您的自动化脚本中使用以下命令来捕获输出:

# 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 &lt;count&gt;-t &lt;time&gt;-T &lt;time&gt; 参数,您可以使用这个更简单的版本,而无需先使用 logcat -c 清除日志:

# 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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 2012-02-04
    • 1970-01-01
    • 2022-08-27
    • 1970-01-01
    相关资源
    最近更新 更多