[위 에] 안 드 로 이 드 프로그램 에서 셸 스 크 립 트 를 어떻게 실행 합 니까?

안 드 로 이 드 애플 리 케 이 션 을 할 때 셸 스 크 립 트 를 실행 하여 특정한 기능 을 신속하게 실현 해 야 합 니 다.
안 드 로 이 드 응용 프로그램 에서 셸 스 크 립 트 를 실행 하면 번 거 로 운 코드 를 줄 일 수 있 고 불필요 한 오 류 를 피 할 수 있 습 니 다.
예 를 들 어 폴 더 를 복사 할 때 셸 명령 의 cp 명령 을 실행 하여 목적 을 달성 할 수 있 습 니 다.코드 에서 폴 더 를 복사 할 때 번 거 로 운 코드 를 많이 작성 해 야 할 뿐만 아니 라 순환 하 는 오류 에 빠 지기 쉽다.
예 를 들 어 파일 시스템 의 읽 기와 쓰기 권한 을 가 져 오 려 면 셸 스 크 립 트 의 한 마디 만 실행 해 야 합 니 다. mount - o rw, remount/쉽게 해결 할 수 있 습 니 다.
예 를 들 어 폴 더 의 다음 파일 이나 같은 종류의 파일 이나 모든 파일 을 삭제 하려 면 셸 스 크 립 트 의 rm - f 만 실행 해 야 합 니 다. *(* 마스크 를 이용 하여 매 칭) 쉽게 해결 할 수 있 습 니 다.
예 를 들 어 침묵 설치 시 셸 스 크 립 트 의 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
"; public final static String COMMAND_LINE_END = "
"; /** * 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; } }

좋은 웹페이지 즐겨찾기