Android实现静默安装的两种方法

  protected static void excuteSuCMD() {

  Process process = null;

  OutputStream out = null;

  InputStream in = null;

  String currentTempFilePath = "/sdcard/QQ.apk";

  try {

  // 请求root

  process = Runtime.getRuntime().exec("su");

  out = process.getOutputStream();

  // 调用安装

  out.write(("pm install -r " + currentTempFilePath + "

  ").getBytes());

  in = process.getInputStream();

  int len = 0;

  byte[] bs = new byte[256];

  while (-1 != (len = in.read(bs))) {

  String state = new String(bs, 0, len);

  if (state.equals("Success

  ")) {

  //安装成功后的操作

  }

  }

  } catch (IOException e) {

  e.printStackTrace();

  } catch (Exception e) {

  e.printStackTrace();

  } finally {

  try {

  if (out != null) {

  out.flush();

  out.close();

  }

  if (in != null) {

  in.close();

  }

  } catch (IOException e) {

  e.printStackTrace();

  }

  }

  }