一.静默安装到system/app目录(要Root权限

 

/**
     * 静默安装到system/app
     * 需要Root权限
     * 见https://zhidao.baidu.com/question/810339575946432852.html
     */
    private void installSlient() {
        String _apkPath = ActivityProgressbarDownloadApk.DOWNLOAD_APK_PATH;
        File _file = new File(_apkPath);
        if(_file.exists()){
            // 移除
            final String cmd_rm = "rm /system/app/techfit_kulian.apk";
            // 拷贝,没有cp工具,可以将cp替换成cat
            final String cmd_cp = "cp ".concat(_apkPath).concat(" /system/app/");
            // 修改app读写权限
            final String cmd_dir_permissions = "chmod 644 /system/app/techfit_kulian.apk";
            // 改为读写权限
            final String cmd_mount_rw = "mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system";
            // 改为只读权限
            final String cmd_mount_ro = "mount -o remount,ro -t yaffs2 /dev/block/mtdblock3 /system";
            Process process = null;
            DataOutputStream os = null;
            BufferedReader successResult = null;
            BufferedReader errorResult = null;
            StringBuilder successMsg = null;
            StringBuilder errorMsg = null;
            try {
                //静默安装需要root权限
                process = Runtime.getRuntime().exec("su");
                os = new DataOutputStream(process.getOutputStream());
                if(USEMOUNT) {
                    // 改为读写
                    os.write(cmd_mount_rw.getBytes());
                    os.writeBytes("\n");
                    os.flush();

                    // 先移除,否则拷贝后桌面不展示启动图标
                    os.write(cmd_rm.getBytes());
                    os.writeBytes("\n");
                    os.flush();

                    // 拷贝
                    os.write(cmd_cp.getBytes());
                    os.writeBytes("\n");
                    os.flush();

                    os.write(cmd_dir_permissions.getBytes());
                    os.writeBytes("\n");
                    os.flush();

                    // 改为读写
                    os.write(cmd_mount_ro.getBytes());
                    os.writeBytes("\n");
                    os.writeBytes("exit\n");
                    os.writeBytes("exit\n");
                    os.flush();
                } else {
                    os.write(cmd_cp.getBytes());
                    os.writeBytes("\n");
                    os.writeBytes("exit\n");
                    os.writeBytes("exit\n");
                    os.flush();
                }
                //执行命令
                process.waitFor();

                //获取返回结果
                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 (Exception e) {
                e.printStackTrace();
            } finally {
                final DataOutputStream finalOs = os;
                final Process finalProcess = process;
                final BufferedReader finalSuccessResult = successResult;
                final BufferedReader finalErrorResult = errorResult;
                new Handler().postDelayed(new Runnable() {// 延时5秒重启(包名为"com.techfit.kulian"的)app
                    @Override
                    public void run() {
                        try {
                            if (finalOs != null) {
                                finalOs.close();
                            }
                            if (finalProcess != null) {
                                finalProcess.destroy();
                            }
                            if (finalSuccessResult != null) {
                                finalSuccessResult.close();
                            }
                            if (finalErrorResult != null) {
                                finalErrorResult.close();
                            }
                            Intent i = getBaseContext().getPackageManager()
                                    .getLaunchIntentForPackage("com.techfit.kulian");
                            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(i);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }, 5000);

            }
        }
    }

 

"com.techfit.kulian"
posted on 2017-06-05 15:08  alex_wongh  阅读(370)  评论(0编辑  收藏  举报