Android程序執行shell腳本


在做Android應用時,經常需要執行shell腳本,以快速實現某些功能;

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

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

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

比如:刪除文件夾下某一個文件、或者某一類文件、或者全部文件,只需要執行shell腳本中的一句 rm -f  *(利用*通配符進行匹配) 就能輕松搞定;

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

如果這些都用代碼來實現,不僅代碼量增加,還容易造成很多bug,吃力不討好!

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腳本來達到目的,可以省去一大堆代碼,避免很多易犯的錯誤,簡潔高效,何樂而不為呢?!

 

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM