Install apps silently, with granted INSTALL_PACKAGES permission

后端 未结 16 2256
梦谈多话
梦谈多话 2020-11-22 10:32

I am trying to silently install apk into the system. My app is located in /system/app and successfully granted permission \"android.permission.INSTALL_PACKAGES\"

Ho

相关标签:
16条回答
  • 2020-11-22 11:04

    You can simply use adb install command to install/update APK silently. Sample code is below

    public static void InstallAPK(String filename){
        File file = new File(filename); 
        if(file.exists()){
            try {   
                String command;
                filename = StringUtil.insertEscape(filename);
                command = "adb install -r " + filename;
                Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
                proc.waitFor();
            } catch (Exception e) {
            e.printStackTrace();
            }
         }
      }
    
    0 讨论(0)
  • 2020-11-22 11:06

    You can use the hidden API android.content.pm.IPackageInstallObserver by reflection:

    public class PackageManagement {
        public static final int INSTALL_REPLACE_EXISTING = 0x00000002;
        public static final int INSTALL_SUCCEEDED = 1;
    
        private static Method installPackageMethod;
        private static Method deletePackageMethod;
    
        static {
            try {
                installPackageMethod = PackageManager.class.getMethod("installPackage", Uri.class, IPackageInstallObserver.class, Integer.TYPE, String.class);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
    
        public static void installPackage(PackageManager pm, Uri mPackageUri, IPackageInstallObserver observer, int installFlags, String installerPackageName) {
            try {
                installPackageMethod.invoke(pm, mPackageUri, observer, installFlags, installerPackageName);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    Import android.content.pm.IPackageInstallObserver into your project. Your app must be system. You must activate the permission android.permission.INSTALL_PACKAGES in your manifest file.

    0 讨论(0)
  • 2020-11-22 11:06

    I checked all the answers, the conclusion seems to be you must have root access to the device first to make it work.

    But then I found these articles very useful. Since I'm making "company-owned" devices.

    How to Update Android App Silently Without User Interaction

    Android Device Owner - Minimal App

    Here is google's the documentation about "managed-device"

    Fully managed device

    0 讨论(0)
  • 2020-11-22 11:11

    Its possible to do silent install on Android 6 and above. Using the function supplied in the answer by Boris Treukhov, ignore everything else in the post, root is not required either.

    Install your app as device admin, you can have full kiosk mode with silent install of updates in the background.

    0 讨论(0)
提交回复
热议问题