PHP操作安卓指令(adb)

提示:实际上是CMD下利用adb命令来操作,来实现文件的自动下发和上传,Windows环境,本地服务器;


前言

需求:通过界面点击按钮,将电脑上文件下发到已连接电脑的所有安卓设备的指定目录;也可以实现安卓设备文件上传回电脑。


一、安装扩展

  1. 下载安装扩展:https://dl.google.com/android/repository/platform-tools-latest-windows.zip
  2. 解压到指定目录。eg:D:\tool\platform-tools
    Linux版本(没安装过,有需求可自行尝试):https://dl.google.com/android/repository/platform-tools-latest-linux.zip

二、修改环境变量

在这里插入图片描述

三、查看是否安装成功

CMD:adb

输出:
Android Debug Bridge version 1.0.41
Version 35.0.2-12147458
Installed as D:\platform-tools\adb.exe
Running on Windows 10.0.22631

四、查看连接设备

CMD :adb devices

输出: #序列号列表,表示设备已经被电脑识别。
List of devices attached
YXJRAEYPI7B6QC8R        unauthorized

未识别到设备
1.设备未打开USB调试模式,不同型号设备打开调试模式请百度
2.荣耀设备识别不到,在设置中搜索【选择USB配置】,然后选择到【音频来源】

这是一个php操作的例子:

$devicesCommond = 'adb devices 2>&1';
exec($devicesCommond, $output);
$output = mb_convert_encoding($output, 'UTF-8', 'GBK');
//读出所有设备序列号之后,可以通过截取的方式 取到准确的序列号信息
foreach ($output as $k => $v) {
    if ($k == 0 || empty($v)) {
        continue;
    }
    $num = substr($v, 0, 16); //序列号
	//业务代码
}

五、复制电脑文件到安卓设备:下发

CMD:
adb push D:/xxx/test.txt storage/emulated/0/xxx/test.txt  2>&1

这是一个php操作的例子:可根据业务循环下发多台设备

$commond = 'adb -s ' . $num . ' push ' . 'D:/xxx/test.txt'. ' storage/emulated/0/Android/data/xxx/test.txt' . '  2>&1';
exec($commond, $output2);
$output2 = mb_convert_encoding($output2, 'UTF-8', 'GBK');

六、复制安卓设备文件到电脑:上传

CMD:把安卓path目录下文件 全部复制到 电脑D:/var/ 下
adb pull  storage/emulated/0/xxx/ D:/xxx/ 2>&1

这是一个php操作的例子:

$commond = 'adb -s ' . $num . ' pull  storage/emulated/0/Android/data/xxx/ ' . 'D:/xxx/' . ' 2>&1';
exec($commond, $output2);
$output2 = mb_convert_encoding($output2, 'UTF-8', 'GBK');

七、删除安卓设备文件

CMD:将xxx下文件全部删除
adb shell rm storage/emulated/0//xxx/

这是一个php操作的例子:

$delCommond = 'adb -s ' . $num. ' shell rm -rf storage/emulated/0/Android/data/xxx/*';
exec($delCommond, $output3);
$output3 = mb_convert_encoding($output3, 'UTF-8', 'GBK');

实际开发中,删除安卓设备文件时候记得先备份到电脑文件夹中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值