1,建立连接
/*** 建立连接** @return*/public static boolean firstCmd() throws IOException {Process process = null;String command = "D:\platform-tools\adb connect 127.0.0.1:7555";//sdk所在位置process = Runtime.getRuntime().exec(command);InputStreamReader ir = new InputStreamReader(process.getInputStream());try {LineNumberReader input = new LineNumberReader(ir);String line;List<String> list = new ArrayList<>();while ((line = input.readLine()) != null) {list.add(line);}input.close();for (String result : list) {if (result.contains("device") || result.contains("already")) {writeLog("adb: "+command );writeLog("adb first cmdmessage: "+result );return true;}System.out.println("message" + result);}} catch (IOException e) {System.err.println("IOException" + e.getMessage());} finally {ir.close();}return false;}
2,赋予权限:
/*** 建立连接** @return*/public static boolean twoCmd() throws IOException {Process process = null;String command = "D:\platform-tools\adb root";//sdk所在位置process = Runtime.getRuntime().exec(command);InputStreamReader ir = new InputStreamReader(process.getInputStream());try {LineNumberReader input = new LineNumberReader(ir);String line;List<String> list = new ArrayList<>();while ((line = input.readLine()) != null) {list.add(line);}for (String result : list) {if (result.equals("adbd is already running as root")) {writeLog("adb: "+command );writeLog("adb tow cmdmessage: "+result );return true;}if (result.contains("device")) {writeLog("adb: "+command );writeLog("adb tow cmdmessage: "+result );return true;}System.out.println("message" + result);}} catch (IOException e) {System.err.println("IOException" + e.getMessage());} finally {ir.close();}return false;}
3,后面的步骤同上,把不同脚本信息的命令通过cmd执行,获取特定的返回messge信息判断程序命令是否执行成功 。
/*** 通过命令启动apk*/public static void startApk() {//定义执行成功标识,如果执行不成功一直执行到成功boolean flag = true;while (flag)try {//执行命令boolean first = firstCmd();if (first) {//执行权限boolean two = twoCmd();if (two) {boolean three = threeCmd();if (three) {fourCmd();fiveCmd();flag = false;}}}} catch (Exception e) {e.printStackTrace();}}
我们之间通过字符串包含的方式判断命令是否执行完成,即可完成命令的执行,从而实现我们java程序去处理adb命令从而实现apk自动更新 。
文章插图
?
故事到这里就结束了 。自动更新完成,喜欢的记得关注和转发 。
推荐阅读
- “顶级”java程序猿手把手教你快速启动Django项目 Django 应用程序
- 系统维护必备技巧:禁用非必要的启动程序,加快电脑启动速度
- 手把手教你进行安卓逆向之篡改apk名称和图标
- Android获取屏幕宽高的四种方式
- AspNet 应用程序热更新升级工具
- 程序员之间神奇的鄙视链,你在其中哪一层?
- 程序员与体面的公务员,到底怎么选?现在有结果了
- 小程序消息推送,订阅消息的实现,定时推送订阅消息功能
- 实战总结的7个C程序,好东西不私藏
- 2022必须拥有Chrome扩展程序 - 浏览器插件,让你上网效率翻倍