Android examples for Android OS:Shell Command
build Mount Command
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager.NameNotFoundException; import android.util.Log; public class Main{ public static String buildMountCommand() { String retstr = ""; CommandResult ret = runCommand("mount", false); if (ret.error.equals("")) { String[] mt = ret.result.split("\n"); for (String m : mt) { if (m.contains("/system")) { String mstr = m; mstr = mstr.replace(" on ", " ").trim(); String[] mele = mstr.split(" "); int cnt = 0; for (String me : mele) { if (cnt >= 2) { break; }//w ww .ja v a2s .c om if (!me.trim().equals("")) { retstr = retstr + " " + me; cnt++; } } break; } } } if (!retstr.equals("")) { retstr = "mount -o remount,rw" + retstr; } return retstr; } @SuppressWarnings("deprecation") public static CommandResult runCommand(String command, boolean root) { if (GlobalInstance.DEBUG) { Log.e("runRootCommand", command); } Process process = null; DataOutputStream os = null; DataInputStream stdout = null; DataInputStream stderr = null; CommandResult ret = new CommandResult(); try { StringBuffer output = new StringBuffer(); StringBuffer error = new StringBuffer(); if (root) { process = Runtime.getRuntime().exec("su"); os = new DataOutputStream(process.getOutputStream()); os.writeBytes(command + "\n"); os.writeBytes("exit\n"); os.flush(); } else { process = Runtime.getRuntime().exec(command); } stdout = new DataInputStream(process.getInputStream()); String line; while ((line = stdout.readLine()) != null) { output.append(line).append('\n'); } stderr = new DataInputStream(process.getErrorStream()); while ((line = stderr.readLine()) != null) { error.append(line).append('\n'); } process.waitFor(); ret.result = output.toString().trim(); ret.error = error.toString().trim(); } catch (Exception e) { ret.result = ""; ret.error = e.getMessage(); } finally { try { if (os != null) { os.close(); } if (stdout != null) { stdout.close(); } if (stderr != null) { stderr.close(); } // if (process != null) { // try { // process.destroy(); // } catch (Throwable th) { // // } // } } catch (Exception e) { ret.result = ""; ret.error = e.getMessage(); } } if (GlobalInstance.DEBUG) { Log.e("runRootCommand-Result", String.format( "result:%s, error:%s", ret.result, ret.error)); } return ret; } }