Android APK 程序实现自动更新,java服务处理无弹窗,终极解决方案( 二 )

 
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自动更新 。

Android APK 程序实现自动更新,java服务处理无弹窗,终极解决方案

文章插图
 
?
故事到这里就结束了 。自动更新完成,喜欢的记得关注和转发 。




推荐阅读