C#使用adb命令實(shí)現(xiàn)控制手機(jī)打開App

完整步驟

  1. 一部安卓手機(jī),并打開USB調(diào)試模式
  2. 下載adb運(yùn)行程序包
  3. 打開VS,新建項(xiàng)目
  4. 將adb程序包放在項(xiàng)目根目錄下
  5. 開始編程

核心代碼示例

/// <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常用命令

  1. 獲取當(dāng)前連接的設(shè)備
adb devices
  1. 獲取設(shè)備序列號
adb get-serialno

adb shell getprop ro.serialno
  1. 打開某個(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    //打開微信
  1. 關(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)閉微信
  1. 獲取手機(jī)系統(tǒng)版本
adb shell getprop ro.build.version.release
  1. 獲取手機(jī)系統(tǒng)SDK版本
adb shell getprop ro.build.version.sdk
  1. 獲取手機(jī)設(shè)備型號
adb -d shell getprop ro.product.model
  1. 獲取手機(jī)廠商名稱
adb -d shell getprop ro.product.brand
  1. 連接遠(yuǎn)程設(shè)備(此處以MuMu模擬器為例)
adb connect 127.0.0.1:7555
  1. 刪除遠(yuǎn)程設(shè)備(此處以MuMu模擬器為例)
adb disconnect 127.0.0.1:7555
  1. 查看當(dāng)前活躍的應(yīng)用及頁面地址
adb shell dumpsys activity activities | sed -En -e '/Running activities/,/Run #0/p'

(未完待續(xù))

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容