[风之空响][经验分享]Android静默安装应用

本帖最后由 风之空响 于 2018-11-9 00:07 编辑

1.实现静默安装应用的话需要系统权限:

  • 在AndroidManifest.xml中添加android:sharedUserId=“android.uid.system”.
  • 使用系统签名文件(platform.pk8、platform.x509.pem,签名文件位于build/target/product/security/目录下)对apk进行签名

2.在AndroidManifest.xml添加相应需要的权限

3.添加函数代码,调用函数进行应用安装

int result = installSilent("com.firefly.testslinet","/mnt/sdcard/123.apk");

前面包名是你实现静默安装的应用的包名,比如我的是testslinet 应用来静默安装/sdcrad/123.apk.那么就是installSilent(“com.firefly.testslinet”,“/mnt/sdcard/123.apk”);

        /**
         * 静默安装
         * @param packageName   调用installSilent函数的应用包名
         * @param filePath     静默安装应用的apk路径
         * @return 0 安装成功
         *         1 文件不存在
         *         2 安装失败
         */
        public static int installSilent(String packageName,String filePath) {
                File file = new File(filePath);
                if (filePath == null || filePath.length() == 0 || file == null || file.length() <= 0 || !file.exists() || !file.isFile()) {
                        return 1;
                }
                //pm install -i 包名 --user 0 apkpath
                String[] args = { "pm", "install","-i",packageName,"--user","0", "-r", filePath };
                ProcessBuilder processBuilder = new ProcessBuilder(args);
                Process process = null;
                BufferedReader successResult = null;
                BufferedReader errorResult = null;
                StringBuilder successMsg = new StringBuilder();
                StringBuilder errorMsg = new StringBuilder();
                int result;
                try {
                        process = processBuilder.start();
                        successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
                        errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                        String s;
                        Log.d("installSilent", "1");
                        while ((s = successResult.readLine()) != null) {
                                successMsg.append(s);
                                Log.d("installSilent", "while successMsg s:" + s);
                        }
                        Log.d("installSilent", "2");
                        while ((s = errorResult.readLine()) != null) {
                                errorMsg.append(s);
                                Log.d("installSilent", "while errorMsg s:" + s);
                        }
                        Log.d("installSilent", "3");
                } catch (IOException e) {
                        Log.d("installSilent", "4");
                        e.printStackTrace();
                } catch (Exception e) {
                        Log.d("installSilent", "5");
                        e.printStackTrace();
                } finally {
                        Log.d("installSilent", "6");
                        try {
                                if (successResult != null) {
                                        successResult.close();
                                }
                                if (errorResult != null) {
                                        errorResult.close();
                                }
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                        if (process != null) {
                                process.destroy();
                        }
                }

                if (successMsg.toString().contains("Success") || successMsg.toString().contains("success")) {
                        result = 0;
                } else {
                        result = 2;
                }
                Log.d("installSilent", "successMsg:" + successMsg + ", ErrorMsg:" + errorMsg);
                return result;
        }