zoukankan      html  css  js  c++  java
  • 静默安装

    一.静默安装到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("
    ");
                        os.flush();
    
                        // 先移除,否则拷贝后桌面不展示启动图标
                        os.write(cmd_rm.getBytes());
                        os.writeBytes("
    ");
                        os.flush();
    
                        // 拷贝
                        os.write(cmd_cp.getBytes());
                        os.writeBytes("
    ");
                        os.flush();
    
                        os.write(cmd_dir_permissions.getBytes());
                        os.writeBytes("
    ");
                        os.flush();
    
                        // 改为读写
                        os.write(cmd_mount_ro.getBytes());
                        os.writeBytes("
    ");
                        os.writeBytes("exit
    ");
                        os.writeBytes("exit
    ");
                        os.flush();
                    } else {
                        os.write(cmd_cp.getBytes());
                        os.writeBytes("
    ");
                        os.writeBytes("exit
    ");
                        os.writeBytes("exit
    ");
                        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"
  • 相关阅读:
    抓包来看ftp状态码
    socket基础篇
    密码复杂度检查函数
    time模块
    读取日志文件,搜索关键字,打印关键字前5行。yield、deque实例
    装饰器--函数
    yield用法
    字符编码
    pycharm + git实现两台电脑代码同步
    PyCharm常见用法
  • 原文地址:https://www.cnblogs.com/bravestarrhu/p/6945047.html
Copyright © 2011-2022 走看看