Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Runtime命令行方式實現APK root權限靜默安裝

Android Runtime命令行方式實現APK root權限靜默安裝

編輯:關於Android編程

本文主要介紹程序如何利用root權限靜默安裝APK,如何自動選擇普通安裝還是靜默安裝以及擴展PackageUtils實現靜默刪除APK。

1、root權限靜默安裝調用

直接調用PackageUtils.installSlient函數(直接引入TrineaAndroidCommon@GoogleCode或TrineaAndroidCommon@Github作為你項目的library),系統授權管理會彈出對話框讓用戶選擇是否允許應用獲得root權限。允許的話即可靜默安裝。該函數返回PackageUtils.INSTALL_SUCCEEDED表示安裝成功,失敗則返回相應錯誤碼,可以得到失敗的詳細原因,包括文件不存在,apk無效,系統內存不足,簽名不正確,缺少公共庫,share user錯誤等等判斷。
注意對於較大apk安裝過程非常耗時,所以最好新啟線程去調用PackageUtils.installSlient。


2、root權限靜默安裝實現

PackageUtils.installSlient的實現實際使用的是su pm install -r filePath命令。核心代碼如下:

PackageUtils.installSlient的實現代碼
  Java
  public static final String COMMAND_SU = "su";
  public static final String COMMAND_SH = "sh";
  public static final String COMMAND_EXIT = "exit\n";
  public static final String COMMAND_LINE_END = "\n";
  public static CommandResult execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) {
  int result = -1;
  if (commands == null || commands.length == 0) {
  return new CommandResult(result, null, null);
  }
  Process process = null;
  BufferedReader successResult = null;
  BufferedReader errorResult = null;
  StringBuilder successMsg = null;
  StringBuilder errorMsg = null;
  DataOutputStream os = null;
  try {
  process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
  os = new DataOutputStream(process.getOutputStream());
  for (String command : commands) {
  if (command == null) {
  continue;
  }
  // donnot use os.writeBytes(commmand), avoid chinese charset error
  os.write(command.getBytes());
  os.writeBytes(COMMAND_LINE_END);
  os.flush();
  }
  os.writeBytes(COMMAND_EXIT);
  os.flush();
  result = process.waitFor();
  // get command result
  if (isNeedResultMsg) {
  successMsg = new StringBuilder();
  errorMsg = new StringBuilder();
  successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
  errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
  String s;
  while ((s = successResult.readLine()) != null) {
  successMsg.append(s);
  }
  while ((s = errorResult.readLine()) != null) {
  errorMsg.append(s);
  }
  }
  } catch (IOException e) {
  e.printStackTrace();
  } catch (Exception e) {
  e.printStackTrace();
  } finally {
  try {
  if (os != null) {
  os.close();
  }
  if (successResult != null) {
  successResult.close();
  }
  if (errorResult != null) {
  errorResult.close();
  }
  } catch (IOException e) {
  e.printStackTrace();
  }
  if (process != null) {
  process.destroy();
  }
  }
  return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null
  : errorMsg.toString());
  }
  public static final String COMMAND_SU = "su";
  public static final String COMMAND_SH = "sh";
  public static final String COMMAND_EXIT = "exit\n";
  public static final String COMMAND_LINE_END = "\n";
  public static CommandResult execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) {
  int result = -1;
  if (commands == null || commands.length == 0) {
  return new CommandResult(result, null, null);
  }
  Process process = null;
  BufferedReader successResult = null;
  BufferedReader errorResult = null;
  StringBuilder successMsg = null;
  StringBuilder errorMsg = null;
  DataOutputStream os = null;
  try {
  process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
  os = new DataOutputStream(process.getOutputStream());
  for (String command : commands) {
  if (command == null) {
  continue;
  }
  // donnot use os.writeBytes(commmand), avoid chinese charset error
  os.write(command.getBytes());
  os.writeBytes(COMMAND_LINE_END);
  os.flush();
  }
  os.writeBytes(COMMAND_EXIT);
  os.flush();
  result = process.waitFor();
  // get command result
  if (isNeedResultMsg) {
  successMsg = new StringBuilder();
  errorMsg = new StringBuilder();
  successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
  errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
  String s;
  while ((s = successResult.readLine()) != null) {
  successMsg.append(s);
  }
  while ((s = errorResult.readLine()) != null) {
  errorMsg.append(s);
  }
  }
  } catch (IOException e) {
  e.printStackTrace();
  } catch (Exception e) {
  e.printStackTrace();
  } finally {
  try {
  if (os != null) {
  os.close();
  }
  if (successResult != null) {
  successResult.close();
  }
  if (errorResult != null) {
  errorResult.close();
  }
  } catch (IOException e) {
  e.printStackTrace();
  }
  if (process != null) {
  process.destroy();
  }
  }
  return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null
  : errorMsg.toString());
  }

其中commands為pm install -r . 從中可以看出主要就是使用su切換到root環境下,再調用pm install -r進行安裝。


3、普通安裝,系統權限靜默安裝,root權限靜默安裝的自動選擇

查看PackageUtils源碼會發現我還提供了其他幾個安裝函數,其中PackageUtils.install函數會根據是否是系統應用以及是否擁有root權限,從而確定調用哪種安裝方式(普通安裝方式、root靜默安裝方式還是系統權限靜默安裝),源碼如下:

Java
  /**
  * install according conditions
  *
  *
  if system application or rooted, see {@link #installSilent(Context, String)}
  *
  else see {@link #installNormal(Context, String)}
  *
  *
  * @param context
  * @param filePath
  * @return
  */
  public static final int install(Context context, String filePath) {
  if (!PackageUtils.isSystemApplication(context)) {
  boolean isRoot = ShellUtils.checkRootPermission();
  if (!isRoot) {
  return installNormal(context, filePath) ? INSTALL_SUCCEEDED : INSTALL_FAILED_INVALID_URI;
  }
  }
  return installSilent(context, filePath);
  }
  /**
  * install according conditions
  *
  *
  if system application or rooted, see {@link #installSilent(Context, String)}
  *
  else see {@link #installNormal(Context, String)}
  *
  *
  * @param context
  * @param filePath
  * @return
  */
  public static final int install(Context context, String filePath) {
  if (!PackageUtils.isSystemApplication(context)) {
  boolean isRoot = ShellUtils.checkRootPermission();
  if (!isRoot) {
  return installNormal(context, filePath) ? INSTALL_SUCCEEDED : INSTALL_FAILED_INVALID_URI;
  }
  }
  return installSilent(context, filePath);
  }


4、擴展PackageUtils 實現靜默卸載應用

沒有時間實現靜默卸載應用的代碼,不過原理都一樣,請參考PackageUtils.installSlient編寫root權限靜默刪除應用代碼。使用pm uninstall [-k] PACKAGE命令即可。


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved