This article describes how to use the root permission to silently install the APK, how to automatically select normal installation or silent installation, and how to expand PackageUtils to delete the APK silently.
1. Silent Installation and call with root permission
Directly call the PackageUtils. installSlient function (directly introduce TrineaAndroidCommon @ GoogleCode or TrineaAndroidCommon @ Github as the library of your project). The system authorization management dialog box appears asking users to select whether to allow the application to obtain root permissions. The installation can be silent if permitted. This function returns PackageUtils. INSTALL_SUCCEEDED indicates that the installation is successful. If the installation fails, the corresponding error code is returned. The detailed cause of the failure is displayed, including the file does not exist, the apk is invalid, the system memory is insufficient, the signature is incorrect, and the public library is missing, share user error and so on.
Note that the installation process for a large apk is time-consuming, so it is best to start a new thread to call PackageUtils. installSlient.
2. Install and implement the root permission silently
PackageUtils. installSlient actually uses the su pm install-r filePath command. The core code is as follows:
PackageUtils. 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) {r Eturn 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(cmd.exe c (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 (isNeedResult Msg) {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(cmd.exe c (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 (isNeedResult Msg) {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 is pm install-r. We can see that we use su to switch to the root environment, and then call pm install-r for installation.
3. Normal installation, Silent Installation of system permissions, and automatic selection of Silent Installation of root permissions
Check the PackageUtils source code and find that I have provided several other installation functions, including PackageUtils. the install function determines which installation method is called based on whether it is a system application or whether it has root permissions. The source code is as follows:
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. Expand PackageUtils to silently uninstall applications
There is no time to silently uninstall the application code, but the principle is the same. See PackageUtils. installSlient to write the root permission to silently Delete the application code. Run the pm uninstall [-k] PACKAGE command.