安卓(Android) 怎么向shell发送命令

2019年10月13日15:21:48安卓(Android) 怎么向shell发送命令已关闭评论 250 1151字阅读3分50秒

1.简介

安卓(Android) 怎么向shell发送命令?——请看下文。

2.示例代码

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

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import android.util.Log;

public class Main{
    private static final String LOG_TAG = ShellHelper.class.getSimpleName();
    
     
    public static List<String> exec(String command) throws IOException,
            SecurityException {
        return exec(command, true);
    }
    
    public static List<String> exec(String command, boolean trimOutput)
            throws IOException, SecurityException {
        List<String> list = new ArrayList<String>();
        BufferedReader stdInput = null;
        try {
            Process p = Runtime.getRuntime().exec(command);
            stdInput = new BufferedReader(new InputStreamReader(
                    p.getInputStream()));
            String s;
            while ((s = stdInput.readLine()) != null) {
                if (trimOutput) {
                    s = s.trim();
                    if (s.length() != 0)
                        list.add(s);
                } else
                    list.add(s);
            }
        } catch (IOException e) {
            Log.e(LOG_TAG, "IOException occurred while executing '"
                    + command + "'.");
            throw e;
        } catch (SecurityException e) {
            Log.e(LOG_TAG, "SecurityException occurred while executing '"
                    + command + "'.");
            throw e;
        } finally {
            stdInput.close();
        }
        return list;
    }
}

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