SoFunction
Updated on 2025-04-05

Two ways to implement silent installation on Android

Preface

Generally speaking, an installation interface will appear when installing apk in Android system. Users can click OK or Cancel to install the apk. However, in actual project requirements, there is a requirement, which is to hope that the apk will be installed in the background (no prompts for the installation interface appear). This installation method is called silent installation. The following article introduces two methods to implement, let’s take a look together.

1. Silent installation of root permissions

What is actually used issu pm install -r filePathOrder.

The core code is as follows:

protected static void excuteSuCMD() { 
   Process process = null; 
   OutputStream out = null; 
   InputStream in = null; 
   String currentTempFilePath = "/sdcard/"; 
   try { 
   // Request root   process = ().exec("su"); 
   out = (); 
   // Call to install   (("pm install -r " + currentTempFilePath + "\n").getBytes()); 
   in = (); 
   int len = 0; 
   byte[] bs = new byte[256]; 
   while (-1 != (len = (bs))) { 
   String state = new String(bs, 0, len); 
   if (("Success\n")) { 
    //Operation after successful installation     } 
    } 
   } catch (IOException e) { 
    (); 
   } catch (Exception e) { 
    (); 
   } finally { 
    try { 
     if (out != null) { 
      (); 
      (); 
     } 
     if (in != null) { 
      (); 
     } 
    } catch (IOException e) { 
     (); 
    } 
   } 
  } 

2. Non-root permissions prompt the user to install, the code is as follows:

public static void openFile() { 
  // The core is the following code  if (!isHasfile()) { 
   downLoadFile(url); 
  } 
  Intent intent = new Intent(); 
  (Intent.FLAG_ACTIVITY_NEW_TASK); 
  (.ACTION_VIEW); 
  ( 
    (new File("/sdcard/update/")), 
    "application/-archive"); 
  (intent); 
 } 

Summarize

The above is all about the implementation of silent installation of Android. I hope that the content of this article will be of some help to everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.