Struct ADB

Source
pub struct ADB {
    pub config: ADBConfig,
    /* private fields */
}
Expand description

ADB 主结构体

Fields§

§config: ADBConfig

Implementations§

Source§

impl ADB

Source

pub fn new(config: Option<ADBConfig>) -> Self

创建新的 ADB 实例

Examples found in repository?
examples/screen_capture.rs (line 4)
3fn main() -> ADBResult<()> {
4    let adb = ADB::new(None);
5
6    // 列出设备
7    let devices = adb.list_devices()?;
8    if devices.is_empty() {
9        println!("没有连接的设备");
10        return Ok(());
11    }
12
13    let device_id = &devices[0].id;
14    println!("使用设备: {}", device_id);
15
16    // 截图
17    let screenshot_path = "screenshot.png";
18    println!("正在截图...");
19    adb.take_screenshot_managed(device_id, screenshot_path)?;
20    println!("截图已保存到: {}", screenshot_path);
21
22    // 录制屏幕
23    let recording_path = "screen_recording.mp4";
24    println!("正在录制屏幕 (5 秒)...");
25    adb.record_screen_managed(device_id, recording_path, 5, None)?;
26    println!("录制已保存到: {}", recording_path);
27
28    Ok(())
29}
More examples
Hide additional examples
examples/parallel_operations.rs (line 4)
3fn main() -> ADBResult<()> {
4    let adb = ADB::new(None);
5
6    // 列出设备
7    let devices = adb.list_devices()?;
8    println!("发现 {} 个设备", devices.len());
9
10    if devices.len() < 2 {
11        println!("此示例需要至少 2 个设备");
12        return Ok(());
13    }
14
15    // 获取设备 ID 列表
16    let device_ids: Vec<&str> = devices.iter().map(|d| d.id.as_str()).collect();
17
18    // 并行执行 shell 命令
19    println!("并行执行 shell 命令...");
20    let shell_results = adb.parallel_shell(&device_ids, "getprop ro.product.model");
21
22    for (id, result) in &shell_results {
23        match result {
24            Ok(output) => println!("设备 {}: 型号 = {}", id, output.trim()),
25            Err(e) => println!("设备 {}: 错误 = {}", id, e),
26        }
27    }
28
29    // 检查多个设备是否在线
30    println!("检查设备在线状态...");
31    let online_devices = adb.filter_online_devices(&device_ids)?;
32    println!("在线设备: {}", online_devices.join(", "));
33
34    // 在所有在线设备上获取电池信息
35    println!("获取所有设备的电池信息...");
36    let battery_results = adb.on_all_online_devices(|device_id| {
37        adb.shell(device_id, "dumpsys battery")
38    })?;
39
40    for (id, result) in battery_results {
41        match result {
42            Ok(_) => println!("设备 {}: 已获取电池信息", id),
43            Err(e) => println!("设备 {}: 获取电池信息失败 = {}", id, e),
44        }
45    }
46
47    Ok(())
48}
examples/file_transfer.rs (line 5)
4fn main() -> ADBResult<()> {
5    let adb = ADB::new(None);
6
7    // 列出设备
8    let devices = adb.list_devices()?;
9    if devices.is_empty() {
10        println!("没有连接的设备");
11        return Ok(());
12    }
13
14    let device_id = &devices[0].id;
15    println!("使用设备: {}", device_id);
16
17    // 推送文件
18    let local_file = "test_file.txt";
19    let device_path = "/sdcard/test_file.txt";
20
21    // 创建测试文件
22    if !Path::new(local_file).exists() {
23        std::fs::write(local_file, "这是一个测试文件内容")?;
24    }
25
26    // 定义传输选项
27    let options = TransferOptions {
28        ..Default::default()
29    };
30
31    // 推送文件
32    println!("推送文件: {} -> {}", local_file, device_path);
33    adb.push(device_id, local_file, device_path, Some(options.clone()))?;
34
35    // 检查文件是否存在
36    let exists = adb.file_exists(device_id, device_path)?;
37    println!("设备上文件存在: {}", exists);
38
39    // 获取文件大小
40    if exists {
41        let size = adb.get_file_size(device_id, device_path)?;
42        println!("文件大小: {} 字节", size);
43    }
44
45    // 拉取文件
46    let local_output = "downloaded_test_file.txt";
47    println!("拉取文件: {} -> {}", device_path, local_output);
48    adb.pull(device_id, device_path, local_output, Some(options))?;
49
50    // 删除设备上的文件
51    println!("删除设备上的文件: {}", device_path);
52    adb.remove_path(device_id, device_path, false)?;
53
54    Ok(())
55}
examples/basic_usage.rs (line 8)
3fn main() -> ADBResult<()> {
4    // 创建配置
5    let config = ADBConfig::default();
6
7    // 创建 ADB 实例
8    let adb = ADB::new(Some(config));
9
10    // 检查 ADB 是否可用
11    match adb.check_adb() {
12        Ok(version) => println!("ADB 版本: {}", version),
13        Err(e) => {
14            eprintln!("ADB 不可用: {}", e);
15            return Err(e);
16        }
17    }
18
19    // 列出连接的设备
20    let devices = adb.list_devices()?;
21    println!("发现 {} 个设备:", devices.len());
22
23    for device in &devices {
24        println!("  ID: {}, 名称: {}, 状态: {}", device.id, device.name, device.status);
25
26        if device.is_online() {
27            // 获取设备属性
28            let android_version = adb.get_prop(&device.id, "ro.build.version.release")?;
29            println!("  Android 版本: {}", android_version);
30
31            // 列出已安装的第三方应用
32            let apps = adb.list_packages(&device.id, false, true)?;
33            println!("  已安装的第三方应用数量: {}", apps.len());
34
35            if !apps.is_empty() {
36                let app = &apps[0];
37                println!("  获取应用信息: {}", app);
38
39                // 获取应用信息
40                if let Ok(info) = adb.get_package_info(&device.id, app) {
41                    if let Some(version) = &info.version_name {
42                        println!("    版本: {}", version);
43                    }
44                    println!("    权限数量: {}", info.permissions.len());
45                }
46
47                // 检查应用是否在运行
48                let (running, pid) = adb.is_package_running(&device.id, app)?;
49                if running {
50                    println!("    应用正在运行, PID: {:?}", pid);
51                } else {
52                    println!("    应用未运行");
53                }
54            }
55        }
56    }
57
58    Ok(())
59}
examples/app_management.rs (line 4)
3fn main() -> ADBResult<()> {
4    let adb = ADB::new(None);
5
6    // 列出设备
7    let devices = adb.list_devices()?;
8    if devices.is_empty() {
9        println!("没有连接的设备");
10        return Ok(());
11    }
12
13    let device_id = &devices[0].id;
14    println!("使用设备: {}", device_id);
15
16    // 列出已安装的第三方应用
17    let apps = adb.list_packages(device_id, false, true)?;
18    println!("已安装的第三方应用数量: {}", apps.len());
19
20    if !apps.is_empty() {
21        let app = &apps[0];
22        println!("选择应用: {}", app);
23
24        // 获取应用信息
25        let info = adb.get_package_info(device_id, app)?;
26        println!("应用信息:");
27        println!("  版本名称: {:?}", info.version_name);
28        println!("  版本代码: {:?}", info.version_code);
29        println!("  安装时间: {:?}", info.install_time);
30        println!("  权限数量: {}", info.permissions.len());
31
32        // 检查应用是否在运行
33        let (running, pid) = adb.is_package_running(device_id, app)?;
34        println!("应用运行状态: {}, PID: {:?}", running, pid);
35
36        // 启动应用
37        if !running {
38            println!("启动应用...");
39            let success = adb.start_app(device_id, app, None)?;
40            println!("启动结果: {}", success);
41
42            // 等待应用启动
43            std::thread::sleep(std::time::Duration::from_secs(2));
44
45            // 再次检查应用是否在运行
46            let (running, pid) = adb.is_package_running(device_id, app)?;
47            println!("应用运行状态: {}, PID: {:?}", running, pid);
48        }
49
50        // 停止应用
51        if running {
52            println!("停止应用...");
53            adb.stop_app(device_id, app)?;
54
55            // 等待应用停止
56            std::thread::sleep(std::time::Duration::from_secs(1));
57
58            // 再次检查应用是否在运行
59            let (running, _) = adb.is_package_running(device_id, app)?;
60            println!("应用运行状态: {}", running);
61        }
62    }
63
64    Ok(())
65}
Source

pub fn adb_path(&self) -> &PathBuf

获取 ADB 路径

Source§

impl ADB

Source

pub fn with_retry<F, T>(&self, f: F) -> ADBResult<T>
where F: Fn() -> ADBResult<T>,

使用指数退避策略重试操作

Source

pub fn with_timeout<F, T>(&self, f: F) -> ADBResult<T>
where F: FnOnce() -> ADBResult<T> + Send + 'static, T: Send + 'static,

带超时的操作执行

Source

pub fn check_adb(&self) -> ADBResult<String>

检查 ADB 是否可用并获取版本

Examples found in repository?
examples/basic_usage.rs (line 11)
3fn main() -> ADBResult<()> {
4    // 创建配置
5    let config = ADBConfig::default();
6
7    // 创建 ADB 实例
8    let adb = ADB::new(Some(config));
9
10    // 检查 ADB 是否可用
11    match adb.check_adb() {
12        Ok(version) => println!("ADB 版本: {}", version),
13        Err(e) => {
14            eprintln!("ADB 不可用: {}", e);
15            return Err(e);
16        }
17    }
18
19    // 列出连接的设备
20    let devices = adb.list_devices()?;
21    println!("发现 {} 个设备:", devices.len());
22
23    for device in &devices {
24        println!("  ID: {}, 名称: {}, 状态: {}", device.id, device.name, device.status);
25
26        if device.is_online() {
27            // 获取设备属性
28            let android_version = adb.get_prop(&device.id, "ro.build.version.release")?;
29            println!("  Android 版本: {}", android_version);
30
31            // 列出已安装的第三方应用
32            let apps = adb.list_packages(&device.id, false, true)?;
33            println!("  已安装的第三方应用数量: {}", apps.len());
34
35            if !apps.is_empty() {
36                let app = &apps[0];
37                println!("  获取应用信息: {}", app);
38
39                // 获取应用信息
40                if let Ok(info) = adb.get_package_info(&device.id, app) {
41                    if let Some(version) = &info.version_name {
42                        println!("    版本: {}", version);
43                    }
44                    println!("    权限数量: {}", info.permissions.len());
45                }
46
47                // 检查应用是否在运行
48                let (running, pid) = adb.is_package_running(&device.id, app)?;
49                if running {
50                    println!("    应用正在运行, PID: {:?}", pid);
51                } else {
52                    println!("    应用未运行");
53                }
54            }
55        }
56    }
57
58    Ok(())
59}
Source

pub fn list_devices(&self) -> ADBResult<Vec<ADBDevice>>

列出可用设备

Examples found in repository?
examples/screen_capture.rs (line 7)
3fn main() -> ADBResult<()> {
4    let adb = ADB::new(None);
5
6    // 列出设备
7    let devices = adb.list_devices()?;
8    if devices.is_empty() {
9        println!("没有连接的设备");
10        return Ok(());
11    }
12
13    let device_id = &devices[0].id;
14    println!("使用设备: {}", device_id);
15
16    // 截图
17    let screenshot_path = "screenshot.png";
18    println!("正在截图...");
19    adb.take_screenshot_managed(device_id, screenshot_path)?;
20    println!("截图已保存到: {}", screenshot_path);
21
22    // 录制屏幕
23    let recording_path = "screen_recording.mp4";
24    println!("正在录制屏幕 (5 秒)...");
25    adb.record_screen_managed(device_id, recording_path, 5, None)?;
26    println!("录制已保存到: {}", recording_path);
27
28    Ok(())
29}
More examples
Hide additional examples
examples/parallel_operations.rs (line 7)
3fn main() -> ADBResult<()> {
4    let adb = ADB::new(None);
5
6    // 列出设备
7    let devices = adb.list_devices()?;
8    println!("发现 {} 个设备", devices.len());
9
10    if devices.len() < 2 {
11        println!("此示例需要至少 2 个设备");
12        return Ok(());
13    }
14
15    // 获取设备 ID 列表
16    let device_ids: Vec<&str> = devices.iter().map(|d| d.id.as_str()).collect();
17
18    // 并行执行 shell 命令
19    println!("并行执行 shell 命令...");
20    let shell_results = adb.parallel_shell(&device_ids, "getprop ro.product.model");
21
22    for (id, result) in &shell_results {
23        match result {
24            Ok(output) => println!("设备 {}: 型号 = {}", id, output.trim()),
25            Err(e) => println!("设备 {}: 错误 = {}", id, e),
26        }
27    }
28
29    // 检查多个设备是否在线
30    println!("检查设备在线状态...");
31    let online_devices = adb.filter_online_devices(&device_ids)?;
32    println!("在线设备: {}", online_devices.join(", "));
33
34    // 在所有在线设备上获取电池信息
35    println!("获取所有设备的电池信息...");
36    let battery_results = adb.on_all_online_devices(|device_id| {
37        adb.shell(device_id, "dumpsys battery")
38    })?;
39
40    for (id, result) in battery_results {
41        match result {
42            Ok(_) => println!("设备 {}: 已获取电池信息", id),
43            Err(e) => println!("设备 {}: 获取电池信息失败 = {}", id, e),
44        }
45    }
46
47    Ok(())
48}
examples/file_transfer.rs (line 8)
4fn main() -> ADBResult<()> {
5    let adb = ADB::new(None);
6
7    // 列出设备
8    let devices = adb.list_devices()?;
9    if devices.is_empty() {
10        println!("没有连接的设备");
11        return Ok(());
12    }
13
14    let device_id = &devices[0].id;
15    println!("使用设备: {}", device_id);
16
17    // 推送文件
18    let local_file = "test_file.txt";
19    let device_path = "/sdcard/test_file.txt";
20
21    // 创建测试文件
22    if !Path::new(local_file).exists() {
23        std::fs::write(local_file, "这是一个测试文件内容")?;
24    }
25
26    // 定义传输选项
27    let options = TransferOptions {
28        ..Default::default()
29    };
30
31    // 推送文件
32    println!("推送文件: {} -> {}", local_file, device_path);
33    adb.push(device_id, local_file, device_path, Some(options.clone()))?;
34
35    // 检查文件是否存在
36    let exists = adb.file_exists(device_id, device_path)?;
37    println!("设备上文件存在: {}", exists);
38
39    // 获取文件大小
40    if exists {
41        let size = adb.get_file_size(device_id, device_path)?;
42        println!("文件大小: {} 字节", size);
43    }
44
45    // 拉取文件
46    let local_output = "downloaded_test_file.txt";
47    println!("拉取文件: {} -> {}", device_path, local_output);
48    adb.pull(device_id, device_path, local_output, Some(options))?;
49
50    // 删除设备上的文件
51    println!("删除设备上的文件: {}", device_path);
52    adb.remove_path(device_id, device_path, false)?;
53
54    Ok(())
55}
examples/basic_usage.rs (line 20)
3fn main() -> ADBResult<()> {
4    // 创建配置
5    let config = ADBConfig::default();
6
7    // 创建 ADB 实例
8    let adb = ADB::new(Some(config));
9
10    // 检查 ADB 是否可用
11    match adb.check_adb() {
12        Ok(version) => println!("ADB 版本: {}", version),
13        Err(e) => {
14            eprintln!("ADB 不可用: {}", e);
15            return Err(e);
16        }
17    }
18
19    // 列出连接的设备
20    let devices = adb.list_devices()?;
21    println!("发现 {} 个设备:", devices.len());
22
23    for device in &devices {
24        println!("  ID: {}, 名称: {}, 状态: {}", device.id, device.name, device.status);
25
26        if device.is_online() {
27            // 获取设备属性
28            let android_version = adb.get_prop(&device.id, "ro.build.version.release")?;
29            println!("  Android 版本: {}", android_version);
30
31            // 列出已安装的第三方应用
32            let apps = adb.list_packages(&device.id, false, true)?;
33            println!("  已安装的第三方应用数量: {}", apps.len());
34
35            if !apps.is_empty() {
36                let app = &apps[0];
37                println!("  获取应用信息: {}", app);
38
39                // 获取应用信息
40                if let Ok(info) = adb.get_package_info(&device.id, app) {
41                    if let Some(version) = &info.version_name {
42                        println!("    版本: {}", version);
43                    }
44                    println!("    权限数量: {}", info.permissions.len());
45                }
46
47                // 检查应用是否在运行
48                let (running, pid) = adb.is_package_running(&device.id, app)?;
49                if running {
50                    println!("    应用正在运行, PID: {:?}", pid);
51                } else {
52                    println!("    应用未运行");
53                }
54            }
55        }
56    }
57
58    Ok(())
59}
examples/app_management.rs (line 7)
3fn main() -> ADBResult<()> {
4    let adb = ADB::new(None);
5
6    // 列出设备
7    let devices = adb.list_devices()?;
8    if devices.is_empty() {
9        println!("没有连接的设备");
10        return Ok(());
11    }
12
13    let device_id = &devices[0].id;
14    println!("使用设备: {}", device_id);
15
16    // 列出已安装的第三方应用
17    let apps = adb.list_packages(device_id, false, true)?;
18    println!("已安装的第三方应用数量: {}", apps.len());
19
20    if !apps.is_empty() {
21        let app = &apps[0];
22        println!("选择应用: {}", app);
23
24        // 获取应用信息
25        let info = adb.get_package_info(device_id, app)?;
26        println!("应用信息:");
27        println!("  版本名称: {:?}", info.version_name);
28        println!("  版本代码: {:?}", info.version_code);
29        println!("  安装时间: {:?}", info.install_time);
30        println!("  权限数量: {}", info.permissions.len());
31
32        // 检查应用是否在运行
33        let (running, pid) = adb.is_package_running(device_id, app)?;
34        println!("应用运行状态: {}, PID: {:?}", running, pid);
35
36        // 启动应用
37        if !running {
38            println!("启动应用...");
39            let success = adb.start_app(device_id, app, None)?;
40            println!("启动结果: {}", success);
41
42            // 等待应用启动
43            std::thread::sleep(std::time::Duration::from_secs(2));
44
45            // 再次检查应用是否在运行
46            let (running, pid) = adb.is_package_running(device_id, app)?;
47            println!("应用运行状态: {}, PID: {:?}", running, pid);
48        }
49
50        // 停止应用
51        if running {
52            println!("停止应用...");
53            adb.stop_app(device_id, app)?;
54
55            // 等待应用停止
56            std::thread::sleep(std::time::Duration::from_secs(1));
57
58            // 再次检查应用是否在运行
59            let (running, _) = adb.is_package_running(device_id, app)?;
60            println!("应用运行状态: {}", running);
61        }
62    }
63
64    Ok(())
65}
Source

pub fn connect(&self, ip: &str, port: u16) -> ADBResult<()>

连接到远程设备

Source

pub fn disconnect(&self, ip: &str, port: Option<u16>) -> ADBResult<()>

断开与远程设备的连接

Source

pub fn disconnect_all(&self) -> ADBResult<()>

断开所有远程连接

Source

pub fn shell(&self, device_id: &str, command: &str) -> ADBResult<String>

在设备上执行 shell 命令

Examples found in repository?
examples/parallel_operations.rs (line 37)
3fn main() -> ADBResult<()> {
4    let adb = ADB::new(None);
5
6    // 列出设备
7    let devices = adb.list_devices()?;
8    println!("发现 {} 个设备", devices.len());
9
10    if devices.len() < 2 {
11        println!("此示例需要至少 2 个设备");
12        return Ok(());
13    }
14
15    // 获取设备 ID 列表
16    let device_ids: Vec<&str> = devices.iter().map(|d| d.id.as_str()).collect();
17
18    // 并行执行 shell 命令
19    println!("并行执行 shell 命令...");
20    let shell_results = adb.parallel_shell(&device_ids, "getprop ro.product.model");
21
22    for (id, result) in &shell_results {
23        match result {
24            Ok(output) => println!("设备 {}: 型号 = {}", id, output.trim()),
25            Err(e) => println!("设备 {}: 错误 = {}", id, e),
26        }
27    }
28
29    // 检查多个设备是否在线
30    println!("检查设备在线状态...");
31    let online_devices = adb.filter_online_devices(&device_ids)?;
32    println!("在线设备: {}", online_devices.join(", "));
33
34    // 在所有在线设备上获取电池信息
35    println!("获取所有设备的电池信息...");
36    let battery_results = adb.on_all_online_devices(|device_id| {
37        adb.shell(device_id, "dumpsys battery")
38    })?;
39
40    for (id, result) in battery_results {
41        match result {
42            Ok(_) => println!("设备 {}: 已获取电池信息", id),
43            Err(e) => println!("设备 {}: 获取电池信息失败 = {}", id, e),
44        }
45    }
46
47    Ok(())
48}
Source

pub fn shell_no_wait(&self, device_id: &str, command: &str) -> ADBResult<()>

执行 shell 命令但不等待完成

Source

pub fn find_device_by_ip(&self, ip: &str) -> ADBResult<Option<String>>

通过 IP 地址查找设备

Source

pub fn get_prop(&self, device_id: &str, prop_name: &str) -> ADBResult<String>

获取设备属性

Examples found in repository?
examples/basic_usage.rs (line 28)
3fn main() -> ADBResult<()> {
4    // 创建配置
5    let config = ADBConfig::default();
6
7    // 创建 ADB 实例
8    let adb = ADB::new(Some(config));
9
10    // 检查 ADB 是否可用
11    match adb.check_adb() {
12        Ok(version) => println!("ADB 版本: {}", version),
13        Err(e) => {
14            eprintln!("ADB 不可用: {}", e);
15            return Err(e);
16        }
17    }
18
19    // 列出连接的设备
20    let devices = adb.list_devices()?;
21    println!("发现 {} 个设备:", devices.len());
22
23    for device in &devices {
24        println!("  ID: {}, 名称: {}, 状态: {}", device.id, device.name, device.status);
25
26        if device.is_online() {
27            // 获取设备属性
28            let android_version = adb.get_prop(&device.id, "ro.build.version.release")?;
29            println!("  Android 版本: {}", android_version);
30
31            // 列出已安装的第三方应用
32            let apps = adb.list_packages(&device.id, false, true)?;
33            println!("  已安装的第三方应用数量: {}", apps.len());
34
35            if !apps.is_empty() {
36                let app = &apps[0];
37                println!("  获取应用信息: {}", app);
38
39                // 获取应用信息
40                if let Ok(info) = adb.get_package_info(&device.id, app) {
41                    if let Some(version) = &info.version_name {
42                        println!("    版本: {}", version);
43                    }
44                    println!("    权限数量: {}", info.permissions.len());
45                }
46
47                // 检查应用是否在运行
48                let (running, pid) = adb.is_package_running(&device.id, app)?;
49                if running {
50                    println!("    应用正在运行, PID: {:?}", pid);
51                } else {
52                    println!("    应用未运行");
53                }
54            }
55        }
56    }
57
58    Ok(())
59}
Source

pub fn set_prop( &self, device_id: &str, prop_name: &str, prop_value: &str, ) -> ADBResult<()>

设置设备属性

Source

pub fn get_all_props( &self, device_id: &str, ) -> ADBResult<HashMap<String, String>>

获取设备所有属性

Source

pub fn is_device_online(&self, device_id: &str) -> ADBResult<bool>

检查设备是否在线

Source

pub fn restart_server(&self) -> ADBResult<()>

重启 ADB 服务器

Source

pub fn run_command(&self, args: &[&str]) -> ADBResult<String>

执行任意 ADB 命令

Source

pub fn wait_for_device( &self, device_id: &str, timeout_ms: Option<u64>, ) -> ADBResult<bool>

等待设备连接

Source

pub fn get_server_version(&self) -> ADBResult<u32>

获取 ADB 服务器版本

Source

pub fn get_pid_optimized( &self, device_id: &str, package_name: &str, ) -> ADBResult<Option<i32>>

优化版的进程 ID 获取

Source

pub fn is_package_running_optimized( &self, device_id: &str, package_name: &str, ) -> ADBResult<(bool, Option<i32>)>

检查包是否运行的优化版本

Source§

impl ADB

Source

pub fn get_package_info( &self, device_id: &str, package_name: &str, ) -> ADBResult<PackageInfo>

获取包信息 (增强版本)

Examples found in repository?
examples/basic_usage.rs (line 40)
3fn main() -> ADBResult<()> {
4    // 创建配置
5    let config = ADBConfig::default();
6
7    // 创建 ADB 实例
8    let adb = ADB::new(Some(config));
9
10    // 检查 ADB 是否可用
11    match adb.check_adb() {
12        Ok(version) => println!("ADB 版本: {}", version),
13        Err(e) => {
14            eprintln!("ADB 不可用: {}", e);
15            return Err(e);
16        }
17    }
18
19    // 列出连接的设备
20    let devices = adb.list_devices()?;
21    println!("发现 {} 个设备:", devices.len());
22
23    for device in &devices {
24        println!("  ID: {}, 名称: {}, 状态: {}", device.id, device.name, device.status);
25
26        if device.is_online() {
27            // 获取设备属性
28            let android_version = adb.get_prop(&device.id, "ro.build.version.release")?;
29            println!("  Android 版本: {}", android_version);
30
31            // 列出已安装的第三方应用
32            let apps = adb.list_packages(&device.id, false, true)?;
33            println!("  已安装的第三方应用数量: {}", apps.len());
34
35            if !apps.is_empty() {
36                let app = &apps[0];
37                println!("  获取应用信息: {}", app);
38
39                // 获取应用信息
40                if let Ok(info) = adb.get_package_info(&device.id, app) {
41                    if let Some(version) = &info.version_name {
42                        println!("    版本: {}", version);
43                    }
44                    println!("    权限数量: {}", info.permissions.len());
45                }
46
47                // 检查应用是否在运行
48                let (running, pid) = adb.is_package_running(&device.id, app)?;
49                if running {
50                    println!("    应用正在运行, PID: {:?}", pid);
51                } else {
52                    println!("    应用未运行");
53                }
54            }
55        }
56    }
57
58    Ok(())
59}
More examples
Hide additional examples
examples/app_management.rs (line 25)
3fn main() -> ADBResult<()> {
4    let adb = ADB::new(None);
5
6    // 列出设备
7    let devices = adb.list_devices()?;
8    if devices.is_empty() {
9        println!("没有连接的设备");
10        return Ok(());
11    }
12
13    let device_id = &devices[0].id;
14    println!("使用设备: {}", device_id);
15
16    // 列出已安装的第三方应用
17    let apps = adb.list_packages(device_id, false, true)?;
18    println!("已安装的第三方应用数量: {}", apps.len());
19
20    if !apps.is_empty() {
21        let app = &apps[0];
22        println!("选择应用: {}", app);
23
24        // 获取应用信息
25        let info = adb.get_package_info(device_id, app)?;
26        println!("应用信息:");
27        println!("  版本名称: {:?}", info.version_name);
28        println!("  版本代码: {:?}", info.version_code);
29        println!("  安装时间: {:?}", info.install_time);
30        println!("  权限数量: {}", info.permissions.len());
31
32        // 检查应用是否在运行
33        let (running, pid) = adb.is_package_running(device_id, app)?;
34        println!("应用运行状态: {}, PID: {:?}", running, pid);
35
36        // 启动应用
37        if !running {
38            println!("启动应用...");
39            let success = adb.start_app(device_id, app, None)?;
40            println!("启动结果: {}", success);
41
42            // 等待应用启动
43            std::thread::sleep(std::time::Duration::from_secs(2));
44
45            // 再次检查应用是否在运行
46            let (running, pid) = adb.is_package_running(device_id, app)?;
47            println!("应用运行状态: {}, PID: {:?}", running, pid);
48        }
49
50        // 停止应用
51        if running {
52            println!("停止应用...");
53            adb.stop_app(device_id, app)?;
54
55            // 等待应用停止
56            std::thread::sleep(std::time::Duration::from_secs(1));
57
58            // 再次检查应用是否在运行
59            let (running, _) = adb.is_package_running(device_id, app)?;
60            println!("应用运行状态: {}", running);
61        }
62    }
63
64    Ok(())
65}
Source

pub fn get_package_info_enhanced( &self, device_id: &str, package_name: &str, ) -> ADBResult<PackageInfo>

获取包信息 (增强版本)

Source

pub fn is_package_running( &self, device_id: &str, package_name: &str, ) -> ADBResult<(bool, Option<i32>)>

检查包是否运行

Examples found in repository?
examples/basic_usage.rs (line 48)
3fn main() -> ADBResult<()> {
4    // 创建配置
5    let config = ADBConfig::default();
6
7    // 创建 ADB 实例
8    let adb = ADB::new(Some(config));
9
10    // 检查 ADB 是否可用
11    match adb.check_adb() {
12        Ok(version) => println!("ADB 版本: {}", version),
13        Err(e) => {
14            eprintln!("ADB 不可用: {}", e);
15            return Err(e);
16        }
17    }
18
19    // 列出连接的设备
20    let devices = adb.list_devices()?;
21    println!("发现 {} 个设备:", devices.len());
22
23    for device in &devices {
24        println!("  ID: {}, 名称: {}, 状态: {}", device.id, device.name, device.status);
25
26        if device.is_online() {
27            // 获取设备属性
28            let android_version = adb.get_prop(&device.id, "ro.build.version.release")?;
29            println!("  Android 版本: {}", android_version);
30
31            // 列出已安装的第三方应用
32            let apps = adb.list_packages(&device.id, false, true)?;
33            println!("  已安装的第三方应用数量: {}", apps.len());
34
35            if !apps.is_empty() {
36                let app = &apps[0];
37                println!("  获取应用信息: {}", app);
38
39                // 获取应用信息
40                if let Ok(info) = adb.get_package_info(&device.id, app) {
41                    if let Some(version) = &info.version_name {
42                        println!("    版本: {}", version);
43                    }
44                    println!("    权限数量: {}", info.permissions.len());
45                }
46
47                // 检查应用是否在运行
48                let (running, pid) = adb.is_package_running(&device.id, app)?;
49                if running {
50                    println!("    应用正在运行, PID: {:?}", pid);
51                } else {
52                    println!("    应用未运行");
53                }
54            }
55        }
56    }
57
58    Ok(())
59}
More examples
Hide additional examples
examples/app_management.rs (line 33)
3fn main() -> ADBResult<()> {
4    let adb = ADB::new(None);
5
6    // 列出设备
7    let devices = adb.list_devices()?;
8    if devices.is_empty() {
9        println!("没有连接的设备");
10        return Ok(());
11    }
12
13    let device_id = &devices[0].id;
14    println!("使用设备: {}", device_id);
15
16    // 列出已安装的第三方应用
17    let apps = adb.list_packages(device_id, false, true)?;
18    println!("已安装的第三方应用数量: {}", apps.len());
19
20    if !apps.is_empty() {
21        let app = &apps[0];
22        println!("选择应用: {}", app);
23
24        // 获取应用信息
25        let info = adb.get_package_info(device_id, app)?;
26        println!("应用信息:");
27        println!("  版本名称: {:?}", info.version_name);
28        println!("  版本代码: {:?}", info.version_code);
29        println!("  安装时间: {:?}", info.install_time);
30        println!("  权限数量: {}", info.permissions.len());
31
32        // 检查应用是否在运行
33        let (running, pid) = adb.is_package_running(device_id, app)?;
34        println!("应用运行状态: {}, PID: {:?}", running, pid);
35
36        // 启动应用
37        if !running {
38            println!("启动应用...");
39            let success = adb.start_app(device_id, app, None)?;
40            println!("启动结果: {}", success);
41
42            // 等待应用启动
43            std::thread::sleep(std::time::Duration::from_secs(2));
44
45            // 再次检查应用是否在运行
46            let (running, pid) = adb.is_package_running(device_id, app)?;
47            println!("应用运行状态: {}, PID: {:?}", running, pid);
48        }
49
50        // 停止应用
51        if running {
52            println!("停止应用...");
53            adb.stop_app(device_id, app)?;
54
55            // 等待应用停止
56            std::thread::sleep(std::time::Duration::from_secs(1));
57
58            // 再次检查应用是否在运行
59            let (running, _) = adb.is_package_running(device_id, app)?;
60            println!("应用运行状态: {}", running);
61        }
62    }
63
64    Ok(())
65}
Source

pub fn get_pid( &self, device_id: &str, package_name: &str, ) -> ADBResult<Option<i32>>

获取进程 ID

Source

pub fn start_app_and_wait( &self, device_id: &str, package_name: &str, activity: Option<&str>, timeout_secs: Option<u64>, ) -> ADBResult<bool>

启动一个应用并等待直到完全启动

Source

pub fn start_app( &self, device_id: &str, package_name: &str, activity: Option<&str>, ) -> ADBResult<bool>

启动应用程序

Examples found in repository?
examples/app_management.rs (line 39)
3fn main() -> ADBResult<()> {
4    let adb = ADB::new(None);
5
6    // 列出设备
7    let devices = adb.list_devices()?;
8    if devices.is_empty() {
9        println!("没有连接的设备");
10        return Ok(());
11    }
12
13    let device_id = &devices[0].id;
14    println!("使用设备: {}", device_id);
15
16    // 列出已安装的第三方应用
17    let apps = adb.list_packages(device_id, false, true)?;
18    println!("已安装的第三方应用数量: {}", apps.len());
19
20    if !apps.is_empty() {
21        let app = &apps[0];
22        println!("选择应用: {}", app);
23
24        // 获取应用信息
25        let info = adb.get_package_info(device_id, app)?;
26        println!("应用信息:");
27        println!("  版本名称: {:?}", info.version_name);
28        println!("  版本代码: {:?}", info.version_code);
29        println!("  安装时间: {:?}", info.install_time);
30        println!("  权限数量: {}", info.permissions.len());
31
32        // 检查应用是否在运行
33        let (running, pid) = adb.is_package_running(device_id, app)?;
34        println!("应用运行状态: {}, PID: {:?}", running, pid);
35
36        // 启动应用
37        if !running {
38            println!("启动应用...");
39            let success = adb.start_app(device_id, app, None)?;
40            println!("启动结果: {}", success);
41
42            // 等待应用启动
43            std::thread::sleep(std::time::Duration::from_secs(2));
44
45            // 再次检查应用是否在运行
46            let (running, pid) = adb.is_package_running(device_id, app)?;
47            println!("应用运行状态: {}, PID: {:?}", running, pid);
48        }
49
50        // 停止应用
51        if running {
52            println!("停止应用...");
53            adb.stop_app(device_id, app)?;
54
55            // 等待应用停止
56            std::thread::sleep(std::time::Duration::from_secs(1));
57
58            // 再次检查应用是否在运行
59            let (running, _) = adb.is_package_running(device_id, app)?;
60            println!("应用运行状态: {}", running);
61        }
62    }
63
64    Ok(())
65}
Source

pub fn stop_app(&self, device_id: &str, package_name: &str) -> ADBResult<()>

强制停止应用程序

Examples found in repository?
examples/app_management.rs (line 53)
3fn main() -> ADBResult<()> {
4    let adb = ADB::new(None);
5
6    // 列出设备
7    let devices = adb.list_devices()?;
8    if devices.is_empty() {
9        println!("没有连接的设备");
10        return Ok(());
11    }
12
13    let device_id = &devices[0].id;
14    println!("使用设备: {}", device_id);
15
16    // 列出已安装的第三方应用
17    let apps = adb.list_packages(device_id, false, true)?;
18    println!("已安装的第三方应用数量: {}", apps.len());
19
20    if !apps.is_empty() {
21        let app = &apps[0];
22        println!("选择应用: {}", app);
23
24        // 获取应用信息
25        let info = adb.get_package_info(device_id, app)?;
26        println!("应用信息:");
27        println!("  版本名称: {:?}", info.version_name);
28        println!("  版本代码: {:?}", info.version_code);
29        println!("  安装时间: {:?}", info.install_time);
30        println!("  权限数量: {}", info.permissions.len());
31
32        // 检查应用是否在运行
33        let (running, pid) = adb.is_package_running(device_id, app)?;
34        println!("应用运行状态: {}, PID: {:?}", running, pid);
35
36        // 启动应用
37        if !running {
38            println!("启动应用...");
39            let success = adb.start_app(device_id, app, None)?;
40            println!("启动结果: {}", success);
41
42            // 等待应用启动
43            std::thread::sleep(std::time::Duration::from_secs(2));
44
45            // 再次检查应用是否在运行
46            let (running, pid) = adb.is_package_running(device_id, app)?;
47            println!("应用运行状态: {}, PID: {:?}", running, pid);
48        }
49
50        // 停止应用
51        if running {
52            println!("停止应用...");
53            adb.stop_app(device_id, app)?;
54
55            // 等待应用停止
56            std::thread::sleep(std::time::Duration::from_secs(1));
57
58            // 再次检查应用是否在运行
59            let (running, _) = adb.is_package_running(device_id, app)?;
60            println!("应用运行状态: {}", running);
61        }
62    }
63
64    Ok(())
65}
Source

pub fn install_app(&self, device_id: &str, apk_path: &str) -> ADBResult<()>

安装应用程序

Source

pub fn uninstall_app( &self, device_id: &str, package_name: &str, ) -> ADBResult<()>

卸载应用程序

Source

pub fn uninstall_app_smart( &self, device_id: &str, package_name: &str, keep_data: bool, ) -> ADBResult<()>

智能卸载应用(清除数据和缓存)

Source

pub fn list_packages( &self, device_id: &str, only_system: bool, only_third_party: bool, ) -> ADBResult<Vec<String>>

获取设备上已安装的应用列表

Examples found in repository?
examples/basic_usage.rs (line 32)
3fn main() -> ADBResult<()> {
4    // 创建配置
5    let config = ADBConfig::default();
6
7    // 创建 ADB 实例
8    let adb = ADB::new(Some(config));
9
10    // 检查 ADB 是否可用
11    match adb.check_adb() {
12        Ok(version) => println!("ADB 版本: {}", version),
13        Err(e) => {
14            eprintln!("ADB 不可用: {}", e);
15            return Err(e);
16        }
17    }
18
19    // 列出连接的设备
20    let devices = adb.list_devices()?;
21    println!("发现 {} 个设备:", devices.len());
22
23    for device in &devices {
24        println!("  ID: {}, 名称: {}, 状态: {}", device.id, device.name, device.status);
25
26        if device.is_online() {
27            // 获取设备属性
28            let android_version = adb.get_prop(&device.id, "ro.build.version.release")?;
29            println!("  Android 版本: {}", android_version);
30
31            // 列出已安装的第三方应用
32            let apps = adb.list_packages(&device.id, false, true)?;
33            println!("  已安装的第三方应用数量: {}", apps.len());
34
35            if !apps.is_empty() {
36                let app = &apps[0];
37                println!("  获取应用信息: {}", app);
38
39                // 获取应用信息
40                if let Ok(info) = adb.get_package_info(&device.id, app) {
41                    if let Some(version) = &info.version_name {
42                        println!("    版本: {}", version);
43                    }
44                    println!("    权限数量: {}", info.permissions.len());
45                }
46
47                // 检查应用是否在运行
48                let (running, pid) = adb.is_package_running(&device.id, app)?;
49                if running {
50                    println!("    应用正在运行, PID: {:?}", pid);
51                } else {
52                    println!("    应用未运行");
53                }
54            }
55        }
56    }
57
58    Ok(())
59}
More examples
Hide additional examples
examples/app_management.rs (line 17)
3fn main() -> ADBResult<()> {
4    let adb = ADB::new(None);
5
6    // 列出设备
7    let devices = adb.list_devices()?;
8    if devices.is_empty() {
9        println!("没有连接的设备");
10        return Ok(());
11    }
12
13    let device_id = &devices[0].id;
14    println!("使用设备: {}", device_id);
15
16    // 列出已安装的第三方应用
17    let apps = adb.list_packages(device_id, false, true)?;
18    println!("已安装的第三方应用数量: {}", apps.len());
19
20    if !apps.is_empty() {
21        let app = &apps[0];
22        println!("选择应用: {}", app);
23
24        // 获取应用信息
25        let info = adb.get_package_info(device_id, app)?;
26        println!("应用信息:");
27        println!("  版本名称: {:?}", info.version_name);
28        println!("  版本代码: {:?}", info.version_code);
29        println!("  安装时间: {:?}", info.install_time);
30        println!("  权限数量: {}", info.permissions.len());
31
32        // 检查应用是否在运行
33        let (running, pid) = adb.is_package_running(device_id, app)?;
34        println!("应用运行状态: {}, PID: {:?}", running, pid);
35
36        // 启动应用
37        if !running {
38            println!("启动应用...");
39            let success = adb.start_app(device_id, app, None)?;
40            println!("启动结果: {}", success);
41
42            // 等待应用启动
43            std::thread::sleep(std::time::Duration::from_secs(2));
44
45            // 再次检查应用是否在运行
46            let (running, pid) = adb.is_package_running(device_id, app)?;
47            println!("应用运行状态: {}, PID: {:?}", running, pid);
48        }
49
50        // 停止应用
51        if running {
52            println!("停止应用...");
53            adb.stop_app(device_id, app)?;
54
55            // 等待应用停止
56            std::thread::sleep(std::time::Duration::from_secs(1));
57
58            // 再次检查应用是否在运行
59            let (running, _) = adb.is_package_running(device_id, app)?;
60            println!("应用运行状态: {}", running);
61        }
62    }
63
64    Ok(())
65}
Source§

impl ADB

Source

pub fn pull( &self, device_id: &str, device_path: &str, local_path: &str, options: Option<TransferOptions>, ) -> ADBResult<()>

文件拉取

Examples found in repository?
examples/file_transfer.rs (line 48)
4fn main() -> ADBResult<()> {
5    let adb = ADB::new(None);
6
7    // 列出设备
8    let devices = adb.list_devices()?;
9    if devices.is_empty() {
10        println!("没有连接的设备");
11        return Ok(());
12    }
13
14    let device_id = &devices[0].id;
15    println!("使用设备: {}", device_id);
16
17    // 推送文件
18    let local_file = "test_file.txt";
19    let device_path = "/sdcard/test_file.txt";
20
21    // 创建测试文件
22    if !Path::new(local_file).exists() {
23        std::fs::write(local_file, "这是一个测试文件内容")?;
24    }
25
26    // 定义传输选项
27    let options = TransferOptions {
28        ..Default::default()
29    };
30
31    // 推送文件
32    println!("推送文件: {} -> {}", local_file, device_path);
33    adb.push(device_id, local_file, device_path, Some(options.clone()))?;
34
35    // 检查文件是否存在
36    let exists = adb.file_exists(device_id, device_path)?;
37    println!("设备上文件存在: {}", exists);
38
39    // 获取文件大小
40    if exists {
41        let size = adb.get_file_size(device_id, device_path)?;
42        println!("文件大小: {} 字节", size);
43    }
44
45    // 拉取文件
46    let local_output = "downloaded_test_file.txt";
47    println!("拉取文件: {} -> {}", device_path, local_output);
48    adb.pull(device_id, device_path, local_output, Some(options))?;
49
50    // 删除设备上的文件
51    println!("删除设备上的文件: {}", device_path);
52    adb.remove_path(device_id, device_path, false)?;
53
54    Ok(())
55}
Source

pub fn push( &self, device_id: &str, local_path: &str, device_path: &str, options: Option<TransferOptions>, ) -> ADBResult<()>

文件推送

Examples found in repository?
examples/file_transfer.rs (line 33)
4fn main() -> ADBResult<()> {
5    let adb = ADB::new(None);
6
7    // 列出设备
8    let devices = adb.list_devices()?;
9    if devices.is_empty() {
10        println!("没有连接的设备");
11        return Ok(());
12    }
13
14    let device_id = &devices[0].id;
15    println!("使用设备: {}", device_id);
16
17    // 推送文件
18    let local_file = "test_file.txt";
19    let device_path = "/sdcard/test_file.txt";
20
21    // 创建测试文件
22    if !Path::new(local_file).exists() {
23        std::fs::write(local_file, "这是一个测试文件内容")?;
24    }
25
26    // 定义传输选项
27    let options = TransferOptions {
28        ..Default::default()
29    };
30
31    // 推送文件
32    println!("推送文件: {} -> {}", local_file, device_path);
33    adb.push(device_id, local_file, device_path, Some(options.clone()))?;
34
35    // 检查文件是否存在
36    let exists = adb.file_exists(device_id, device_path)?;
37    println!("设备上文件存在: {}", exists);
38
39    // 获取文件大小
40    if exists {
41        let size = adb.get_file_size(device_id, device_path)?;
42        println!("文件大小: {} 字节", size);
43    }
44
45    // 拉取文件
46    let local_output = "downloaded_test_file.txt";
47    println!("拉取文件: {} -> {}", device_path, local_output);
48    adb.pull(device_id, device_path, local_output, Some(options))?;
49
50    // 删除设备上的文件
51    println!("删除设备上的文件: {}", device_path);
52    adb.remove_path(device_id, device_path, false)?;
53
54    Ok(())
55}
Source

pub fn push_large_file( &self, device_id: &str, local_path: &str, device_path: &str, options: Option<TransferOptions>, ) -> ADBResult<()>

分块推送大文件

Source

pub fn file_exists(&self, device_id: &str, path: &str) -> ADBResult<bool>

文件存在性检查

Examples found in repository?
examples/file_transfer.rs (line 36)
4fn main() -> ADBResult<()> {
5    let adb = ADB::new(None);
6
7    // 列出设备
8    let devices = adb.list_devices()?;
9    if devices.is_empty() {
10        println!("没有连接的设备");
11        return Ok(());
12    }
13
14    let device_id = &devices[0].id;
15    println!("使用设备: {}", device_id);
16
17    // 推送文件
18    let local_file = "test_file.txt";
19    let device_path = "/sdcard/test_file.txt";
20
21    // 创建测试文件
22    if !Path::new(local_file).exists() {
23        std::fs::write(local_file, "这是一个测试文件内容")?;
24    }
25
26    // 定义传输选项
27    let options = TransferOptions {
28        ..Default::default()
29    };
30
31    // 推送文件
32    println!("推送文件: {} -> {}", local_file, device_path);
33    adb.push(device_id, local_file, device_path, Some(options.clone()))?;
34
35    // 检查文件是否存在
36    let exists = adb.file_exists(device_id, device_path)?;
37    println!("设备上文件存在: {}", exists);
38
39    // 获取文件大小
40    if exists {
41        let size = adb.get_file_size(device_id, device_path)?;
42        println!("文件大小: {} 字节", size);
43    }
44
45    // 拉取文件
46    let local_output = "downloaded_test_file.txt";
47    println!("拉取文件: {} -> {}", device_path, local_output);
48    adb.pull(device_id, device_path, local_output, Some(options))?;
49
50    // 删除设备上的文件
51    println!("删除设备上的文件: {}", device_path);
52    adb.remove_path(device_id, device_path, false)?;
53
54    Ok(())
55}
Source

pub fn get_file_size(&self, device_id: &str, path: &str) -> ADBResult<u64>

获取文件/目录大小

Examples found in repository?
examples/file_transfer.rs (line 41)
4fn main() -> ADBResult<()> {
5    let adb = ADB::new(None);
6
7    // 列出设备
8    let devices = adb.list_devices()?;
9    if devices.is_empty() {
10        println!("没有连接的设备");
11        return Ok(());
12    }
13
14    let device_id = &devices[0].id;
15    println!("使用设备: {}", device_id);
16
17    // 推送文件
18    let local_file = "test_file.txt";
19    let device_path = "/sdcard/test_file.txt";
20
21    // 创建测试文件
22    if !Path::new(local_file).exists() {
23        std::fs::write(local_file, "这是一个测试文件内容")?;
24    }
25
26    // 定义传输选项
27    let options = TransferOptions {
28        ..Default::default()
29    };
30
31    // 推送文件
32    println!("推送文件: {} -> {}", local_file, device_path);
33    adb.push(device_id, local_file, device_path, Some(options.clone()))?;
34
35    // 检查文件是否存在
36    let exists = adb.file_exists(device_id, device_path)?;
37    println!("设备上文件存在: {}", exists);
38
39    // 获取文件大小
40    if exists {
41        let size = adb.get_file_size(device_id, device_path)?;
42        println!("文件大小: {} 字节", size);
43    }
44
45    // 拉取文件
46    let local_output = "downloaded_test_file.txt";
47    println!("拉取文件: {} -> {}", device_path, local_output);
48    adb.pull(device_id, device_path, local_output, Some(options))?;
49
50    // 删除设备上的文件
51    println!("删除设备上的文件: {}", device_path);
52    adb.remove_path(device_id, device_path, false)?;
53
54    Ok(())
55}
Source

pub fn create_directory(&self, device_id: &str, path: &str) -> ADBResult<()>

创建目录

Source

pub fn remove_path( &self, device_id: &str, path: &str, recursive: bool, ) -> ADBResult<()>

删除文件或目录

Examples found in repository?
examples/file_transfer.rs (line 52)
4fn main() -> ADBResult<()> {
5    let adb = ADB::new(None);
6
7    // 列出设备
8    let devices = adb.list_devices()?;
9    if devices.is_empty() {
10        println!("没有连接的设备");
11        return Ok(());
12    }
13
14    let device_id = &devices[0].id;
15    println!("使用设备: {}", device_id);
16
17    // 推送文件
18    let local_file = "test_file.txt";
19    let device_path = "/sdcard/test_file.txt";
20
21    // 创建测试文件
22    if !Path::new(local_file).exists() {
23        std::fs::write(local_file, "这是一个测试文件内容")?;
24    }
25
26    // 定义传输选项
27    let options = TransferOptions {
28        ..Default::default()
29    };
30
31    // 推送文件
32    println!("推送文件: {} -> {}", local_file, device_path);
33    adb.push(device_id, local_file, device_path, Some(options.clone()))?;
34
35    // 检查文件是否存在
36    let exists = adb.file_exists(device_id, device_path)?;
37    println!("设备上文件存在: {}", exists);
38
39    // 获取文件大小
40    if exists {
41        let size = adb.get_file_size(device_id, device_path)?;
42        println!("文件大小: {} 字节", size);
43    }
44
45    // 拉取文件
46    let local_output = "downloaded_test_file.txt";
47    println!("拉取文件: {} -> {}", device_path, local_output);
48    adb.pull(device_id, device_path, local_output, Some(options))?;
49
50    // 删除设备上的文件
51    println!("删除设备上的文件: {}", device_path);
52    adb.remove_path(device_id, device_path, false)?;
53
54    Ok(())
55}
Source

pub fn copy_on_device( &self, device_id: &str, src_path: &str, dst_path: &str, ) -> ADBResult<()>

复制设备上的文件

Source

pub fn move_on_device( &self, device_id: &str, src_path: &str, dst_path: &str, ) -> ADBResult<()>

移动设备上的文件

Source

pub fn list_directory( &self, device_id: &str, path: &str, ) -> ADBResult<Vec<String>>

列出目录内容

Source

pub fn get_file_mtime(&self, device_id: &str, path: &str) -> ADBResult<String>

获取文件最后修改时间

Source

pub fn get_available_space(&self, device_id: &str, path: &str) -> ADBResult<u64>

检查设备上的可用空间

Source

pub fn compute_md5(&self, device_id: &str, path: &str) -> ADBResult<String>

计算文件或目录的 MD5 校验和

Source

pub fn write_text_to_file( &self, device_id: &str, path: &str, content: &str, ) -> ADBResult<()>

写入文本到设备上的文件

Source

pub fn read_text_from_file( &self, device_id: &str, path: &str, ) -> ADBResult<String>

读取设备上文件的文本内容

Source

pub fn compare_files( &self, device_id: &str, local_path: &str, device_path: &str, ) -> ADBResult<bool>

比较本地文件和设备文件是否相同

Source

pub fn sync_directory_to_device( &self, device_id: &str, local_dir: &str, device_dir: &str, exclude_patterns: Option<&[&str]>, ) -> ADBResult<()>

同步目录 (本地到设备)

Source§

impl ADB

Source

pub fn enable_remote_debugging( &self, device_id: &str, port: u16, ) -> ADBResult<String>

启用设备远程调试

Source

pub fn get_device_architecture(&self, device_id: &str) -> ADBResult<String>

获取设备架构

Source

pub fn start_frida_server( &self, device_id: &str, frida_server_path: &str, port: u16, server_name: Option<&str>, use_root: Option<bool>, ) -> ADBResult<()>

在设备上启动 Frida 服务器

Source

pub fn stop_frida_server( &self, device_id: &str, server_name: Option<&str>, ) -> ADBResult<()>

停止在设备上运行的 Frida 服务器

Source

pub fn reboot(&self, device_id: &str) -> ADBResult<()>

重启设备到正常模式

Source

pub fn reboot_recovery(&self, device_id: &str) -> ADBResult<()>

重启设备到恢复模式

Source

pub fn reboot_bootloader(&self, device_id: &str) -> ADBResult<()>

重启设备到引导加载程序模式

Source§

impl ADB

Source

pub fn take_screenshot( &self, device_id: &str, output_path: &str, ) -> ADBResult<()>

从设备截图

Source

pub fn record_screen( &self, device_id: &str, output_path: &str, duration_secs: u32, size: Option<&str>, ) -> ADBResult<()>

录制设备屏幕

§参数
  • device_id - 设备 ID
  • output_path - 本地输出路径
  • duration_secs - 录制时长(秒),最大 180 秒
  • size - 可选的分辨率,格式 “widthxheight”
Source

pub fn capture_logs( &self, device_id: &str, tag: Option<&str>, priority: &str, ) -> ADBResult<String>

从设备捕获日志

Source

pub fn watch_logs( &self, device_id: &str, tag: Option<&str>, priority: &str, ) -> ADBResult<()>

实时查看日志(返回立即执行的命令)

Source

pub fn clear_logs(&self, device_id: &str) -> ADBResult<()>

清除日志

Source§

impl ADB

Source

pub fn forward( &self, device_id: &str, local_port: u16, device_port: u16, ) -> ADBResult<()>

将本地端口转发到设备端口

Source

pub fn remove_forward(&self, local_port: u16) -> ADBResult<()>

移除端口转发

Source

pub fn remove_all_forwards(&self) -> ADBResult<()>

移除所有端口转发

Source

pub fn list_forwards(&self) -> ADBResult<String>

列出所有端口转发

Source

pub fn reverse( &self, device_id: &str, remote_port: u16, local_port: u16, ) -> ADBResult<()>

反向端口转发(设备到主机)

Source

pub fn remove_reverse(&self, device_id: &str, remote_port: u16) -> ADBResult<()>

移除反向端口转发

Source

pub fn remove_all_reverses(&self, device_id: &str) -> ADBResult<()>

移除所有反向端口转发

Source§

impl ADB

Source

pub fn create_resource_manager(&self, device_id: &str) -> ResourceManager

创建资源管理器

Source

pub fn with_resources<F, T>(&self, device_id: &str, f: F) -> ADBResult<T>
where F: FnOnce(&mut ResourceManager) -> ADBResult<T>,

使用资源管理器执行操作

Source

pub fn take_screenshot_managed( &self, device_id: &str, output_path: &str, ) -> ADBResult<()>

优化的截图功能(使用资源管理器)

Examples found in repository?
examples/screen_capture.rs (line 19)
3fn main() -> ADBResult<()> {
4    let adb = ADB::new(None);
5
6    // 列出设备
7    let devices = adb.list_devices()?;
8    if devices.is_empty() {
9        println!("没有连接的设备");
10        return Ok(());
11    }
12
13    let device_id = &devices[0].id;
14    println!("使用设备: {}", device_id);
15
16    // 截图
17    let screenshot_path = "screenshot.png";
18    println!("正在截图...");
19    adb.take_screenshot_managed(device_id, screenshot_path)?;
20    println!("截图已保存到: {}", screenshot_path);
21
22    // 录制屏幕
23    let recording_path = "screen_recording.mp4";
24    println!("正在录制屏幕 (5 秒)...");
25    adb.record_screen_managed(device_id, recording_path, 5, None)?;
26    println!("录制已保存到: {}", recording_path);
27
28    Ok(())
29}
Source

pub fn record_screen_managed( &self, device_id: &str, output_path: &str, duration_secs: u32, size: Option<&str>, ) -> ADBResult<()>

优化的屏幕录制功能(使用资源管理器)

Examples found in repository?
examples/screen_capture.rs (line 25)
3fn main() -> ADBResult<()> {
4    let adb = ADB::new(None);
5
6    // 列出设备
7    let devices = adb.list_devices()?;
8    if devices.is_empty() {
9        println!("没有连接的设备");
10        return Ok(());
11    }
12
13    let device_id = &devices[0].id;
14    println!("使用设备: {}", device_id);
15
16    // 截图
17    let screenshot_path = "screenshot.png";
18    println!("正在截图...");
19    adb.take_screenshot_managed(device_id, screenshot_path)?;
20    println!("截图已保存到: {}", screenshot_path);
21
22    // 录制屏幕
23    let recording_path = "screen_recording.mp4";
24    println!("正在录制屏幕 (5 秒)...");
25    adb.record_screen_managed(device_id, recording_path, 5, None)?;
26    println!("录制已保存到: {}", recording_path);
27
28    Ok(())
29}
Source

pub fn with_temp_file<F, T>( &self, device_id: &str, prefix: &str, suffix: &str, f: F, ) -> ADBResult<T>
where F: FnOnce(&str) -> ADBResult<T>,

使用临时文件执行操作

Source§

impl ADB

Source

pub fn parallel_shell( &self, device_ids: &[&str], command: &str, ) -> HashMap<String, ADBResult<String>>

在多个设备上并行执行 shell 命令

§参数
  • device_ids - 设备 ID 列表
  • command - 要执行的 shell 命令
§返回值

返回一个 HashMap,键为设备 ID,值为命令执行结果

Examples found in repository?
examples/parallel_operations.rs (line 20)
3fn main() -> ADBResult<()> {
4    let adb = ADB::new(None);
5
6    // 列出设备
7    let devices = adb.list_devices()?;
8    println!("发现 {} 个设备", devices.len());
9
10    if devices.len() < 2 {
11        println!("此示例需要至少 2 个设备");
12        return Ok(());
13    }
14
15    // 获取设备 ID 列表
16    let device_ids: Vec<&str> = devices.iter().map(|d| d.id.as_str()).collect();
17
18    // 并行执行 shell 命令
19    println!("并行执行 shell 命令...");
20    let shell_results = adb.parallel_shell(&device_ids, "getprop ro.product.model");
21
22    for (id, result) in &shell_results {
23        match result {
24            Ok(output) => println!("设备 {}: 型号 = {}", id, output.trim()),
25            Err(e) => println!("设备 {}: 错误 = {}", id, e),
26        }
27    }
28
29    // 检查多个设备是否在线
30    println!("检查设备在线状态...");
31    let online_devices = adb.filter_online_devices(&device_ids)?;
32    println!("在线设备: {}", online_devices.join(", "));
33
34    // 在所有在线设备上获取电池信息
35    println!("获取所有设备的电池信息...");
36    let battery_results = adb.on_all_online_devices(|device_id| {
37        adb.shell(device_id, "dumpsys battery")
38    })?;
39
40    for (id, result) in battery_results {
41        match result {
42            Ok(_) => println!("设备 {}: 已获取电池信息", id),
43            Err(e) => println!("设备 {}: 获取电池信息失败 = {}", id, e),
44        }
45    }
46
47    Ok(())
48}
Source

pub fn parallel_install_app( &self, device_ids: &[&str], apk_path: &str, ) -> HashMap<String, ADBResult<()>>

在多个设备上并行安装应用

§参数
  • device_ids - 设备 ID 列表
  • apk_path - APK 文件路径
§返回值

返回一个 HashMap,键为设备 ID,值为安装结果

Source

pub fn parallel_uninstall_app( &self, device_ids: &[&str], package_name: &str, ) -> HashMap<String, ADBResult<()>>

在多个设备上并行卸载应用

§参数
  • device_ids - 设备 ID 列表
  • package_name - 包名
§返回值

返回一个 HashMap,键为设备 ID,值为卸载结果

Source

pub fn parallel_start_app( &self, device_ids: &[&str], package_name: &str, activity: Option<&str>, ) -> HashMap<String, ADBResult<bool>>

在多个设备上并行启动应用

§参数
  • device_ids - 设备 ID 列表
  • package_name - 包名
  • activity - 可选的 Activity 名称
§返回值

返回一个 HashMap,键为设备 ID,值为启动结果

Source

pub fn parallel_stop_app( &self, device_ids: &[&str], package_name: &str, ) -> HashMap<String, ADBResult<()>>

在多个设备上并行停止应用

§参数
  • device_ids - 设备 ID 列表
  • package_name - 包名
§返回值

返回一个 HashMap,键为设备 ID,值为停止结果

Source

pub fn parallel_get_package_info( &self, device_ids: &[&str], package_name: &str, ) -> HashMap<String, ADBResult<PackageInfo>>

在多个设备上并行获取包信息

§参数
  • device_ids - 设备 ID 列表
  • package_name - 包名
§返回值

返回一个 HashMap,键为设备 ID,值为包信息

Source

pub fn parallel_push( &self, device_ids: &[&str], local_path: &str, device_path: &str, ) -> HashMap<String, ADBResult<()>>

在多个设备上并行执行推送文件操作

§参数
  • device_ids - 设备 ID 列表
  • local_path - 本地文件路径
  • device_path - 设备上的目标路径
§返回值

返回一个 HashMap,键为设备 ID,值为推送结果

Source

pub fn parallel_pull( &self, operations: &[(String, String, String)], ) -> HashMap<String, ADBResult<()>>

在多个设备上并行执行拉取文件操作

§参数
  • operations - 设备 ID 和文件路径的组合列表,每项包含(设备 ID, 设备文件路径, 本地目标路径)
§返回值

返回一个 HashMap,键为设备 ID,值为拉取结果

Source

pub fn filter_online_devices( &self, device_ids: &[&str], ) -> ADBResult<Vec<String>>

检查多个设备是否在线

§参数
  • device_ids - 设备 ID 列表
§返回值

返回在线设备的列表

Examples found in repository?
examples/parallel_operations.rs (line 31)
3fn main() -> ADBResult<()> {
4    let adb = ADB::new(None);
5
6    // 列出设备
7    let devices = adb.list_devices()?;
8    println!("发现 {} 个设备", devices.len());
9
10    if devices.len() < 2 {
11        println!("此示例需要至少 2 个设备");
12        return Ok(());
13    }
14
15    // 获取设备 ID 列表
16    let device_ids: Vec<&str> = devices.iter().map(|d| d.id.as_str()).collect();
17
18    // 并行执行 shell 命令
19    println!("并行执行 shell 命令...");
20    let shell_results = adb.parallel_shell(&device_ids, "getprop ro.product.model");
21
22    for (id, result) in &shell_results {
23        match result {
24            Ok(output) => println!("设备 {}: 型号 = {}", id, output.trim()),
25            Err(e) => println!("设备 {}: 错误 = {}", id, e),
26        }
27    }
28
29    // 检查多个设备是否在线
30    println!("检查设备在线状态...");
31    let online_devices = adb.filter_online_devices(&device_ids)?;
32    println!("在线设备: {}", online_devices.join(", "));
33
34    // 在所有在线设备上获取电池信息
35    println!("获取所有设备的电池信息...");
36    let battery_results = adb.on_all_online_devices(|device_id| {
37        adb.shell(device_id, "dumpsys battery")
38    })?;
39
40    for (id, result) in battery_results {
41        match result {
42            Ok(_) => println!("设备 {}: 已获取电池信息", id),
43            Err(e) => println!("设备 {}: 获取电池信息失败 = {}", id, e),
44        }
45    }
46
47    Ok(())
48}
Source

pub fn on_all_online_devices<F, T>( &self, operation: F, ) -> ADBResult<HashMap<String, ADBResult<T>>>
where F: Fn(&str) -> ADBResult<T> + Send + Sync, T: Send,

在所有在线设备上执行操作

§参数
  • operation - 要执行的操作闭包
§返回值

返回在线设备的操作结果

Examples found in repository?
examples/parallel_operations.rs (lines 36-38)
3fn main() -> ADBResult<()> {
4    let adb = ADB::new(None);
5
6    // 列出设备
7    let devices = adb.list_devices()?;
8    println!("发现 {} 个设备", devices.len());
9
10    if devices.len() < 2 {
11        println!("此示例需要至少 2 个设备");
12        return Ok(());
13    }
14
15    // 获取设备 ID 列表
16    let device_ids: Vec<&str> = devices.iter().map(|d| d.id.as_str()).collect();
17
18    // 并行执行 shell 命令
19    println!("并行执行 shell 命令...");
20    let shell_results = adb.parallel_shell(&device_ids, "getprop ro.product.model");
21
22    for (id, result) in &shell_results {
23        match result {
24            Ok(output) => println!("设备 {}: 型号 = {}", id, output.trim()),
25            Err(e) => println!("设备 {}: 错误 = {}", id, e),
26        }
27    }
28
29    // 检查多个设备是否在线
30    println!("检查设备在线状态...");
31    let online_devices = adb.filter_online_devices(&device_ids)?;
32    println!("在线设备: {}", online_devices.join(", "));
33
34    // 在所有在线设备上获取电池信息
35    println!("获取所有设备的电池信息...");
36    let battery_results = adb.on_all_online_devices(|device_id| {
37        adb.shell(device_id, "dumpsys battery")
38    })?;
39
40    for (id, result) in battery_results {
41        match result {
42            Ok(_) => println!("设备 {}: 已获取电池信息", id),
43            Err(e) => println!("设备 {}: 获取电池信息失败 = {}", id, e),
44        }
45    }
46
47    Ok(())
48}
Source

pub fn parallel_commands( &self, device_ids: &[&str], commands: &[&str], ) -> HashMap<String, Vec<ADBResult<String>>>

在所有指定设备上并行执行多个命令

Source

pub fn start_app_on_all_devices( &self, package_name: &str, activity: Option<&str>, ) -> ADBResult<HashMap<String, ADBResult<bool>>>

在所有在线设备上启动同一应用

Source

pub fn stop_app_on_all_devices( &self, package_name: &str, ) -> ADBResult<HashMap<String, ADBResult<()>>>

在所有在线设备上停止同一应用

Trait Implementations§

Source§

impl Clone for ADB

Source§

fn clone(&self) -> ADB

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ADB

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for ADB

§

impl RefUnwindSafe for ADB

§

impl Send for ADB

§

impl Sync for ADB

§

impl Unpin for ADB

§

impl UnwindSafe for ADB

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V