示例
現在以Android SDK的adb工具的使用作為例子。
我們這里首先有個functions_adb.bat作為工具類的bat文件的代碼,里面總共封裝了獲取連接的設備列表、推送文件、安裝apk、啟動app共4個函數:
@echo off
set exeAdb=F:\AndroidSDK\platform-tools\adb.exe
set szStoragePath=/storage/emulated/0
set szTargetPrePath=!szStoragePath!/Android/data
call %*
goto :end
setlocal EnableDelayedExpansion
:get_device_list
set aDevices=
for /f "tokens=1 skip=1 delims= " %%d in ('%exeAdb% devices -l') do (
set szDevice=%%d
@REM echo szDevice = !szDevice!
set "aDevices=!aDevices! !szDevice!"
)
echo aDevices = !aDevices!
set "%1=!aDevices!"
goto :eof
@REM ======================================================================================================
:push_file
set "szDevice=%~1"
set "szSrcPath=%~2"
set "szDstPath=%~3"
if "!szSrcPath!" == "" goto :eof
set "szAdbCommand=%exeAdb% -s !szDevice!"
if "!szDevice!" == "" (
set "szAdbCommand=%exeAdb%"
)
call !szAdbCommand! push !szSrcPath! !szStoragePath!/!szDstPath!/
goto :eof
@REM ======================================================================================================
:install_apk
set "szDevice=%~1"
set "szApkPath=%~2"
if "!szApkPath!" == "" goto :eof
set "szAdbCommand=%exeAdb% -s !szDevice!"
if "!szDevice!" == "" (
set "szAdbCommand=%exeAdb%"
)
call !szAdbCommand! install -r "!szApkPath!"
goto :eof
@REM ======================================================================================================
:launch_app
set "szDevice=%~1"
set "szActivityPkg=%~2"
if "!szActivityPkg!" == "" goto :eof
set "szAdbCommand=%exeAdb% -s !szDevice!"
if "!szDevice!" == "" (
set "szAdbCommand=%exeAdb%"
)
call !szAdbCommand! shell am start "!szActivityPkg!"
goto :eof
@REM ======================================================================================================
:end
exit /b
而后假設,我們需要獲取連接的多個安卓機器,然后將指定的apk逐一安裝到各個機子上,可以有這么一段代碼:
@echo off
setlocal EnableDelayedExpansion
set "szApkPath=f:/myapp.apk"
set retDevices=
call functions_adb :get_device_list retDevices
for %%d in (!retDevices!) do (
set szDevice=%%d
call functions_adb :install_apk "!szDevice!" "!szApkPath!"
echo.
)
我們需要保存functions_adb.bat,就能方便使用,而且后續(xù)還能繼續(xù)添加所需的函數。
只要上面這段路徑與functions_adb.bat路徑一樣,或者調用時拼接它的路徑。
結尾
基于簡單的函數設計,可以為自己保存一些對第三方工具通用的封裝,方便日后的工作使用。