完整步骤
- 一部安卓手机,并打开USB调试模式
- 下载adb运行程序包
- 打开VS,新建项目
- 将adb程序包放在项目根目录下
- 开始编程
核心代码示例
/// <summary>
/// 执行adb命令
/// </summary>
/// <param name="arguments">命令参数(adb除外)</param>
/// <returns></returns>
public string RunADB(string arguments)
{
string cmd = Application.StartupPath + "\\adb\\adb.exe";
Process p = new Process();
p.StartInfo.FileName = cmd; //设定程序名
p.StartInfo.Arguments = arguments; //设定程式执行參數
p.StartInfo.UseShellExecute = false; //关闭Shell的使用
p.StartInfo.RedirectStandardInput = true; //重定向标准输入
p.StartInfo.RedirectStandardOutput = true; //重定向标准输出
p.StartInfo.RedirectStandardError = true; //重定向错误输出
p.StartInfo.CreateNoWindow = true; //设置不显示窗口
p.Start();
string result = p.StandardOutput.ReadToEnd();
p.Close();
return result;
}
adb常用命令
- 获取当前连接的设备
adb devices
- 获取设备序列号
adb get-serialno
或
adb shell getprop ro.serialno
- 打开某个App(此处以"学习强国App"为例)
adb shell am start -n cn.xuexi.android/com.alibaba.android.rimet.biz.SplashActivity //打开学习强国
adb shell am start -n com.tencent.mm/.ui.LauncherUI //打开微信
- 关闭某个App(此处以"学习强国App"为例)
adb shell am force-stop cn.xuexi.android //关闭学习强国
adb shell am force-stop com.tencent.mm //关闭微信
- 获取手机系统版本
adb shell getprop ro.build.version.release
- 获取手机系统SDK版本
adb shell getprop ro.build.version.sdk
- 获取手机设备型号
adb -d shell getprop ro.product.model
- 获取手机厂商名称
adb -d shell getprop ro.product.brand
- 连接远程设备(此处以MuMu模拟器为例)
adb connect 127.0.0.1:7555
- 删除远程设备(此处以MuMu模拟器为例)
adb disconnect 127.0.0.1:7555
- 查看当前活跃的应用及页面地址
adb shell dumpsys activity activities | sed -En -e '/Running activities/,/Run #0/p'
(未完待续)