1.前提是手机需要被ROOT,也就是安卓设备需要被ROOT。需要知道apk文件的保存路径(绝对路径)
请看java代码

    //需要传下载后的apk的绝对路径和当前上下文,这个方法的返回值表示为:安装成功是true,安装失败是false
    //apkPath:apk文件的绝对路径
    public static boolean clientInstall(String apkPath,Context context) {
        boolean result = false;
        DataOutputStream dataOutputStream = null;
        BufferedReader errorStream = null;
        BufferedReader successStream = null;
        Process process = null;
        try {
            // 申请 su 权限
            process = Runtime.getRuntime().exec("su");
            dataOutputStream = new DataOutputStream(process.getOutputStream());
            // 执行 pm install 命令  (这是apk安装命令)
            String command = "pm install -r " + apkPath + "\n";
            //执行  am start -n   命令   (这是apk重启命令)    -----.MainActivity是默认重启app后要打开哪个界面
            String runApp = "am start -n  "+context.getPackageName()+"/"+context.getPackageName()+".MainActivity >>/dev/null 2>&1;";
            //这样子写也是可以的,用\n结尾也可以
            // String runApp = "am start -n  "+context.getPackageName()+"/"+context.getPackageName()+".MainActivity"+ "\n";
            command=command+runApp;
            Log.d("111333","走到这里,---------------打印静默安装命令="+command);

            dataOutputStream.write(command.getBytes(Charset.forName("UTF-8")));
            dataOutputStream.writeBytes("exit\n");
            Log.d("111333","走到这里,---------------打印静默安装完成");

            dataOutputStream.flush();
            process.waitFor();
            errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            StringBuilder errorMsg = new StringBuilder();
            String line;
            while ((line = errorStream.readLine()) != null) {
                errorMsg.append(line);
            }
            Log.d("111333","走到这里,---------------silent install error message:{}="+errorMsg);

            StringBuilder successMsg = new StringBuilder();
            successStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
            // 读取命令执行结果
            while ((line = successStream.readLine()) != null) {
                successMsg.append(line);
            }
            Log.d("111333","走到这里,---------------silent install success message:{}="+successMsg);

            // 如果执行结果中包含 Failure 字样就认为是操作失败,否则就认为安装成功
            if (!(errorMsg.toString().contains("Failure") || successMsg.toString().contains("Failure"))) {
                result = true;
            }
        } catch (Exception e) {
            Log.d("111333","走到这里,---------------e="+e);
        } finally {
            try {
                if (process != null) {
                    process.destroy();
                }
                if (dataOutputStream != null) {
                    dataOutputStream.close();
                }
                if (errorStream != null) {
                    errorStream.close();
                }
                if (successStream != null) {
                    successStream.close();
                }
            } catch (Exception e) {
                // ignored
            }
        }
        return result;
    }
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐