Execute Shell Command List - Android Android OS

Android examples for Android OS:Shell Command

Description

Execute Shell Command List

Demo Code


//package com.java2s;

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;

import java.util.List;

public class Main {
    public static List<String> ExecShell_List(String cmd) {
        List<String> result = new ArrayList<String>();
        try {//w ww  . java 2s. c om
            Process p = Runtime.getRuntime().exec("su");
            OutputStream outputStream = p.getOutputStream();

            DataOutputStream dataOutputStream = new DataOutputStream(
                    outputStream);
            dataOutputStream.writeBytes(cmd);
            dataOutputStream.flush();
            dataOutputStream.close();
            outputStream.close();

            InputStream inputStream = p.getInputStream();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(inputStream));
            String line = null;
            while ((line = reader.readLine()) != null) {
                result.add(line);
            }
            inputStream.close();
            return result;
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return result;
    }
}

Related Tutorials