安卓(Android) 怎么通过 Shell 安装 RO

2019年10月13日15:15:32安卓(Android) 怎么通过 Shell 安装 RO已关闭评论 288 2231字阅读7分26秒

1.简介

安卓(Android) 怎么通过 Shell 安装 RO ?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.util.Log;

public class Main{
    private static final String TAG = "ttttttt";
    private static String SYSTEM_PREFIX = null;
    public static boolean mountRO() {
        String prefix = getSystemPrefix();
        if (prefix != null) {
            String command = "mount -o remount,ro " + prefix;
            CommandResult result = execute(command, true);
            return result.exitValue == 0;
        }
        return false;
    }
    private static String getSystemPrefix() {
        if (SYSTEM_PREFIX == null) {
            synchronized (CommandUtils.class) {
                CommandResult result = execute("mount", false);
                String output = result.output;
                if (result.exitValue == 0 && output.length() > 0) {
                    String flag = " /system ";
                    for (String line : output.split("\n")) {
                        if (line.contains(flag)) {
                            SYSTEM_PREFIX = line.substring(0,
                                    line.indexOf(flag) + flag.length());
                        }
                    }
                }
            }
        }
        return SYSTEM_PREFIX;
    }
    public static CommandResult execute(String commands, boolean runAsRoot) {
        CommandResult result = new CommandResult();
        if (commands != null) {
            String[] lines = commands.split("\n");
            Process process = null;
            DataOutputStream os = null;
            try {
                
                process = Runtime.getRuntime().exec(
                        runAsRoot ? "/system/xbin/su" : "sh"); 
                os = new DataOutputStream(process.getOutputStream());
                for (String line : lines) {
                    line = line.trim();
                    if (line.length() > 0 && !line.startsWith("#")) {
                        Log.i(TAG, line);
                        os.writeBytes(line + "\n");
                    }
                }
                os.writeBytes("exit\n");
                os.flush();
                String output = getSteamText(process.getInputStream());
                String error = getSteamText(process.getErrorStream());
                if (process.waitFor() != 0) {
                    result.exitValue = process.exitValue();
                    Log.e(TAG, "exit value = " + result.exitValue);
                }
                result.error = error;
                result.output = output;
                Log.i(TAG, output);
                Log.e(TAG, error);
            } catch (Exception e) {
                Log.e(TAG, e.getMessage(), e);
            } finally {
                try {
                    if (os != null) {
                        os.close();
                    }
                    process.destroy();
                } catch (Exception e) {
                    Log.e(TAG, e.getMessage(), e);
                }
            }
        }
        return result;
    }
    private static String getSteamText(InputStream inputstream)
            throws IOException {
        StringBuilder inputText = new StringBuilder();
        InputStreamReader inputstreamreader = new InputStreamReader(
                inputstream);
        BufferedReader bufferedreader = new BufferedReader(
                inputstreamreader);
        
        String line;
        while ((line = bufferedreader.readLine()) != null) {
            
            inputText.append(line);
            inputText.append('\n');
        }
        return inputText.toString();
    }
}

 
继续阅读
编程十万个怎么办