靜默安裝

一.靜默安裝到system/app目錄(要Root權限html

 

/**
     * 靜默安裝到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"
相關文章
相關標籤/搜索