Android程序執行shell腳本

在作Android應用時,常常須要執行shell腳本,以快速實現某些功能;java

在Android應用程序中執行shell腳本能夠省去一大堆繁瑣的代碼,還能夠避免沒必要要的錯誤;android

好比:拷貝文件夾時,能夠執行shell命令中的 cp 命令達到目的;而在代碼中實現拷貝文件夾時,不只須要編寫一大堆繁瑣的代碼,還容易陷入遞歸死循環的錯誤中;shell

好比:獲取文件系統的讀寫權限,只須要執行shell腳本中一句 mount -o rw,remount / 就能輕鬆搞定;app

好比:刪除文件夾下某一個文件、或者某一類文件、或者所有文件,只須要執行shell腳本中的一句 rm -f  *(利用*通配符進行匹配) 就能輕鬆搞定;工具

再好比:靜默安裝時,只須要執行shell腳本中一句 pm install -r 即可達到目的;ui

若是這些都用代碼來實現,不只代碼量增長,還容易形成不少bug,吃力不討好!spa

package com.example.test;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import android.util.Log;

/**
 * 執行shell腳本工具類
 * @author Mountain
 *
 */
public class CommandExecution {

    public static final String TAG = "CommandExecution";
    
    public final static String COMMAND_SU       = "su";
    public final static String COMMAND_SH       = "sh";
    public final static String COMMAND_EXIT     = "exit\n";
    public final static String COMMAND_LINE_END = "\n";

    /**
     * Command執行結果
     * @author Mountain
     *
     */
    public static class CommandResult {
        public int result = -1;
        public String errorMsg;
        public String successMsg;
    }

    /**
     * 執行命令—單條
     * @param command
     * @param isRoot
     * @return
     */
    public static CommandResult execCommand(String command, boolean isRoot) {
        String[] commands = {command};
        return execCommand(commands, isRoot);
    }

    /**
     * 執行命令-多條
     * @param commands
     * @param isRoot
     * @return
     */
    public static CommandResult execCommand(String[] commands, boolean isRoot) {
        CommandResult commandResult = new CommandResult();
        if (commands == null || commands.length == 0) return commandResult;
        Process process = null;
        DataOutputStream os = null;
        BufferedReader successResult = null;
        BufferedReader errorResult = null;
        StringBuilder successMsg = null;
        StringBuilder errorMsg = null;
        try {
            process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
            os = new DataOutputStream(process.getOutputStream());
            for (String command : commands) {
                if (command != null) {
                    os.write(command.getBytes());
                    os.writeBytes(COMMAND_LINE_END);
                    os.flush();
                }
            }
            os.writeBytes(COMMAND_EXIT);
            os.flush();
            commandResult.result = 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);
            commandResult.successMsg = successMsg.toString();
            commandResult.errorMsg = errorMsg.toString();
            Log.i(TAG, commandResult.result + " | " + commandResult.successMsg
                    + " | " + commandResult.errorMsg);
        } catch (IOException e) {
            String errmsg = e.getMessage();
            if (errmsg != null) {
                Log.e(TAG, errmsg);
            } else {
                e.printStackTrace();
            }
        } catch (Exception e) {
            String errmsg = e.getMessage();
            if (errmsg != null) {
                Log.e(TAG, errmsg);
            } else {
                e.printStackTrace();
            }
        } finally {
            try {
                if (os != null) os.close();
                if (successResult != null) successResult.close();
                if (errorResult != null) errorResult.close();
            } catch (IOException e) {
                String errmsg = e.getMessage();
                if (errmsg != null) {
                    Log.e(TAG, errmsg);
                } else {
                    e.printStackTrace();
                }
            }
            if (process != null) process.destroy();
        }
        return commandResult;
    }
    
}

 

 

若是能在android應用中執行shell腳原本達到目的,能夠省去一大堆代碼,避免不少易犯的錯誤,簡潔高效,何樂而不爲呢?!code

 

下面給出一個在Android應用中執行shell腳本的工具類的示例,供你們參考:blog

相關文章
相關標籤/搜索