新手上路,请多关照。如本文有何不足之处,还望前辈们多多指教!
本文来通过实现一个从页面点击按钮,然后删除Android设备上指定文件的功能来简单介绍一下从页面操作Android设备的流程。
首先,下面的代码是当点击“删除”按钮后触发的js脚本
function(){ //该方法省略了一些代码,只保留核心代码... var device = ''; var devices = adbCommand.getDevice(); //得到当前与电脑连接中的Android设备的序列号 //这个getDevice()方法的实现见下面 if(!devices.length > 0) { alert("请插入终端设备!"); }else{ if(devices.length > 0){ device = devices[0]; //调用删除功能的函数,并得到执行结果 var result = adbCommand.deletePad(device);//deletePad()方法实现见下文 //通过执行结果判断是否执行成功,0代表成功,1代表失败 if(result == 0 || result == "0" || result == '0') { alert("删除成功"); } else { alert("删除失败"); } } } } /** 获取张端设备 **/ function getDevice() { //调用相应方法,执行adb devices这条命令,以查出Android设备的序列号 var result = exec("adb devices"); var line = result.split('\n'); var res = new Array(); if(line.length > 0) { for(var i =1; i<line.length; i++) { if(line[i]) { var temp = line[i].replace(/(^\s*)|(\s*$)/g, ""); if(temp) { res[i-1] = line[i].split('\t')[0]; } } } } return res; } /** 执行传入的命令 **/ function exec(cmd) { var ret = getWSShell().exec(cmd); var result = ret.StdOut.ReadAll(); return result; } /** 得到wscript.shell对象 **/ function getWSShell() { WSShell = new ActiveXObject( "WScript.shell" ); return this.WSShell; } function deletePad(cmd) { //将要执行的.bat文件的路径及其参数拼接成字符串,然后执行 return execFile("D:/oa/DeletePad.bat " + cmd); } function execFile(cmdFile) { var iReturnCode = getWSShell().run(cmdFile, 0, true); return iReturnCode; }
到此为止,js脚本部分就写完了,下面看一下要执行的.bat文件的内容
set CLASSPATH=%CLASSPATH%;D:\oa\ctop.jar java com.xxx.xxx.xxx.DataTranfer 2 %1
下面看这个DataTranfer类的代码,其中省略掉无关的代码,只保留重要的
public class DataTranfer { private static final String SERVER_TO_PAD = "0"; private static final String PAD_TO_SERVER = "1"; private static final String DELETE_PAD = "2"; private ADBCommand adb; private String remoteDeletePath = "sdcard/ZC/"; /** * 主函数 * * @param args args[0] (0:功能1; 1:功能2; 2:删除功能 ) * args[1] (设备号) * args[2] (待办号(用逗号分隔的字符串)) * @return */ public static void main(String[] args) throws Exception { if ((args.length == 3 && DataTranfer.SERVER_TO_PAD.equals(args[0])) || (args.length == 2 && DataTranfer.PAD_TO_SERVER.equals(args[0])) || args.length == 2 && DataTranfer.DELETE_PAD.equals(args[0])) { // 参数个数检测正常 System.out.println(args + "参数检测正常"); } else { throw new Exception();//参数异常,退出 } if (DataTranfer.SERVER_TO_PAD.equals(args[0])) { DataTranfer dt = new DataTranfer(); dt.downLoadDataToPad(args[1], args[2]); } else if (DataTranfer.PAD_TO_SERVER.equals(args[0])) { DataTranfer dt = new DataTranfer(); dt.uploadDataToServer(args[1]); } else if (DataTranfer.DELETE_PAD.equals(args[0])) { DataTranfer dt = new DataTranfer(); dt.deleteDataFromPad(args[1]); } } /** * 这里就是初始化一些属性,不用关心 */ private void initVariables() { adb = new ADBCommand();//这个类是执行命令的类,代码见下文 serverUrl = ConfigManager.getInstance().getServerBaseUrl(); localUpPath = ConfigManager.getInstance().getLocalUpPath(); remoteUpPath = ConfigManager.getInstance().getRemoteUpPath(); remoteDownPath = ConfigManager.getInstance().getRemoteDownPath(); localDownPath = ConfigManager.getInstance().getLocalDownPath(); remoteDeletePath = ConfigManager.getInstance().getRemoteDeletePath(); } private String downLoadDataToPad(String deviceKey, String pendingSnos) { //内容省略…… } private String uploadDataToServer(String deviceKey) { //内容省略…… } /** * 将PAD中的离线数据删除 * * @param deviceKey Android设备序列号 * @return */ private String deleteDataFromPad(String deviceKey){ String result = "删除成功!"; initVariables(); if (!"".equals(deviceKey.trim())) { adb.setDevice(deviceKey); //删除指定路径下的所有后缀为ini的文件 adb.exec(" rm -r " + remoteDeletePath + "*.ini"); //删除指定路径下的所有文件 adb.exec(" rm -r " + remoteDeletePath + "/upload/*"); }else{ result = "没有可用终端!"; } return result; } }
public class ADBCommand { private Runtime runtime; private String device; /** * Default constructor, just get the runtime */ public ADBCommand() { runtime = Runtime.getRuntime(); } /** * Constructor with device initialization * * @param device attached device */ public ADBCommand(String device) { this(); this.device = device; } /** * Set device * * @param device */ public void setDevice(String device) { this.device = device; } /** * Execute a command in a shell * * @param command command to execute * @return the return of the command */ public String exec(String command) { String retOur = ""; try { Process p = runtime.exec("adb -s " + device + " shell " + command); final InputStream is1 = p.getInputStream(); final InputStream is2 = p.getErrorStream(); new Thread() { public void run() { BufferedReader standardIn = new BufferedReader(new InputStreamReader(is1)); String line; try { while ((line = standardIn.readLine()) != null) { if (line != null) { } } } catch (IOException e) { e.printStackTrace(); } } }.start(); new Thread() { public void run() { BufferedReader errorIn = new BufferedReader(new InputStreamReader(is2)); String line; try { while ((line = errorIn.readLine()) != null) { if (line != null) { } } } catch (IOException e) { e.printStackTrace(); } } }.start(); p.waitFor(); p.destroy(); retOur = "执行成功"; } catch (IOException e) { System.out.println(e.getMessage()); } catch (InterruptedException e) { System.out.println(e.getMessage()); } return retOur; } //省略部分代码…… }
D:/oa/DeletePad.bat 4df79c8167df4029
其中,4df79c8167df4029是我的Android设备号,是由js里的getDevice()函数得到的
之后,DeletePad.bat将会被启动,它将调用DataTranfer类的main方法,传入的参数是 2 和 4df79c8167df4029 这两个参数
再之后,DataTranfer类就会拼接成相应的删除命令并执行。最终,我Android设备中ZC文件夹下的所有.ini文件和ZC文件夹中upload文件夹中的所有文件都会被删除