sudo No Wait - Android Android OS

Android examples for Android OS:Shell

Description

sudo No Wait

Demo Code


//package com.java2s;

import java.io.File;
import java.io.IOException;

import java.io.OutputStreamWriter;
import java.util.ArrayList;

import java.util.List;
import java.util.Map;

public class Main {
    private final static String[] BINARY_PLACES = { "/data/bin/",
            "/system/bin/", "/system/xbin/", "/sbin/", "/data/local/xbin/",
            "/data/local/bin/", "/system/sd/xbin/",
            "/system/bin/failsafe/", "/data/local/" };
    private static Boolean IS_ROOTED = null;

    public static Process sudoNoWait(Map<String, String> env,
            String... command) throws Exception {
        if (Boolean.FALSE.equals(IS_ROOTED)) {
            return executeNoWait(env, command);
        }/*w ww. j a  v a  2 s . co m*/
        //        LogUtils.i("sudo: " + Arrays.toString(command));
        ProcessBuilder processBuilder = new ProcessBuilder();
        Process process = processBuilder.command(findCommand("su"))
                .redirectErrorStream(true).start();
        OutputStreamWriter stdin = new OutputStreamWriter(
                process.getOutputStream());
        try {
            for (Map.Entry<String, String> entry : env.entrySet()) {
                stdin.write(entry.getKey());
                stdin.write("=");
                stdin.write(entry.getValue());
                stdin.write(" ");
            }
            for (String c : command) {
                stdin.write(c);
                stdin.write(" ");
            }
            stdin.write("\nexit\n");
        } finally {
            stdin.close();
        }
        return process;
    }

    public static Process executeNoWait(Map<String, String> env,
            String... command) throws IOException {
        //        LogUtils.i("command: " + Arrays.toString(command));
        List<String> envp = new ArrayList<String>();
        for (Map.Entry<String, String> entry : env.entrySet()) {
            envp.add(entry.getKey() + "=" + entry.getValue());
        }
        return Runtime.getRuntime().exec(command,
                envp.toArray(new String[envp.size()]));
    }

    public static String findCommand(String command) {
        for (String binaryPlace : BINARY_PLACES) {
            String path = binaryPlace + command;
            if (new File(path).exists()) {
                return path;
            }
        }
        return command;
    }
}

Related Tutorials