下面介绍一种简易的获取Android UI中的text内容的方法,
1. 使用uiautomator获取UI的xml信息,并解析其中的text字段,会产生/data/window_dump/ui_info.txt
#!/bin/sh
#***********************************************************************
#********** fetch txt information from android UI ******
#***********************************************************************
# remove the remained files
rm /sdcard/window_dump.xml
rm -rf /data/window_dump
# dump the UI XML file
uiautomator dump
mkdir /data/window_dump
# Replace (" )(" and space) to "return", so it can split in multiple lines
sed 's/" /\n/g' /sdcard/window_dump.xml > /data/window_dump/2.txt
# filter lines with "text="
cat /data/window_dump/2.txt | grep "text=" > /data/window_dump/4.txt
# delete "text="
sed 's/text="//g' /data/window_dump/4.txt > /data/window_dump/5.txt
# Replace " " to "return", so it can split in multiple lines
sed 's/ /\n/g' /data/window_dump/5.txt > /data/window_dump/6.txt
# remove blank line
egrep -v "^$" /data/window_dump/6.txt > /data/window_dump/ui_info.txt
2. 在Windows写个batch将上面的script push到Android中运行,batch内容如下,
adb push dump_ui_info.sh /data/dump_ui_info.sh
adb shell "chmod a+x /data/dump_ui_info.sh"
adb shell /data/dump_ui_info.sh
adb pull /data/window_dump/ui_info.txt
pause
执行上面的batch档案,就可以在当前目录下产生ui_info.txt.