完整步驟
- 一部安卓手機(jī),并打開USB調(diào)試模式
- 下載adb運(yùn)行程序包
- 打開VS,新建項(xiàng)目
- 將adb程序包放在項(xiàng)目根目錄下
- 開始編程
核心代碼示例
/// <summary>
/// 執(zhí)行adb命令
/// </summary>
/// <param name="arguments">命令參數(shù)(adb除外)</param>
/// <returns></returns>
public string RunADB(string arguments)
{
string cmd = Application.StartupPath + "\\adb\\adb.exe";
Process p = new Process();
p.StartInfo.FileName = cmd; //設(shè)定程序名
p.StartInfo.Arguments = arguments; //設(shè)定程式執(zhí)行參數(shù)
p.StartInfo.UseShellExecute = false; //關(guān)閉Shell的使用
p.StartInfo.RedirectStandardInput = true; //重定向標(biāo)準(zhǔn)輸入
p.StartInfo.RedirectStandardOutput = true; //重定向標(biāo)準(zhǔn)輸出
p.StartInfo.RedirectStandardError = true; //重定向錯(cuò)誤輸出
p.StartInfo.CreateNoWindow = true; //設(shè)置不顯示窗口
p.Start();
string result = p.StandardOutput.ReadToEnd();
p.Close();
return result;
}
adb常用命令
- 獲取當(dāng)前連接的設(shè)備
adb devices
- 獲取設(shè)備序列號
adb get-serialno
或
adb shell getprop ro.serialno
- 打開某個(gè)App(此處以"學(xué)習(xí)強(qiáng)國App"為例)
adb shell am start -n cn.xuexi.android/com.alibaba.android.rimet.biz.SplashActivity //打開學(xué)習(xí)強(qiáng)國
adb shell am start -n com.tencent.mm/.ui.LauncherUI //打開微信
- 關(guān)閉某個(gè)App(此處以"學(xué)習(xí)強(qiáng)國App"為例)
adb shell am force-stop cn.xuexi.android //關(guān)閉學(xué)習(xí)強(qiáng)國
adb shell am force-stop com.tencent.mm //關(guān)閉微信
- 獲取手機(jī)系統(tǒng)版本
adb shell getprop ro.build.version.release
- 獲取手機(jī)系統(tǒng)SDK版本
adb shell getprop ro.build.version.sdk
- 獲取手機(jī)設(shè)備型號
adb -d shell getprop ro.product.model
- 獲取手機(jī)廠商名稱
adb -d shell getprop ro.product.brand
- 連接遠(yuǎn)程設(shè)備(此處以MuMu模擬器為例)
adb connect 127.0.0.1:7555
- 刪除遠(yuǎn)程設(shè)備(此處以MuMu模擬器為例)
adb disconnect 127.0.0.1:7555
- 查看當(dāng)前活躍的應(yīng)用及頁面地址
adb shell dumpsys activity activities | sed -En -e '/Running activities/,/Run #0/p'
(未完待續(xù))