| /** * * @param apkFilePath 安装包路径 * @return true 、false */ public boolean installByADB(String apkFilePath){ boolean success = false; if (apkFilePath == null || apkFilePath.equals("")) {return success;} File apkFile = new File(apkFilePath); if (!apkFile.exists() || apkFile.isDirectory()) {return success;} String[] args = { "pm", "install", "-r", "-d", apkFilePath }; ByteArrayOutputStream eBAout = new ByteArrayOutputStream(); ByteArrayOutputStream nBAout = new ByteArrayOutputStream(); ProcessBuilder processBuilder = null; java.lang.Process process = null; InputStream eis = null; InputStream is = null; int num = 0; byte[] buffer = new byte[1024]; try { processBuilder = new ProcessBuilder(args); process = processBuilder.start(); eis = process.getErrorStream(); while ((num = eis.read(buffer)) != -1) { eBAout.write(buffer, 0, num); } is = process.getInputStream(); while ((num = is.read(buffer)) != -1) { nBAout.write(buffer, 0, num); } String error = eBAout.toString("UTF-8"); String normal = nBAout.toString("UTF-8"); if(normal != null && normal.contains("Success")){success = true;} } catch (IOException e) { } catch (Exception e) { } catch (Throwable t) { } finally { try { if (null != eis) {eis.close();} } catch (Exception e) {} try { if (null != is) {is.close();} } catch (Exception e) {} process.destroy(); } return success; } |