如题所述
androidä¸æ§è¡shellå½ä»¤æ两ç§æ¹å¼ï¼
1.ç´æ¥å¨ä»£ç ä¸ç¨javaæä¾çRuntime è¿ä¸ªç±»æ¥æ§è¡å½ä»¤ï¼ä»¥ä¸ä¸ºå®æ´ç¤ºä¾ä»£ç ã
public void execCommand(String command) throws IOException {
// start the ls command running
//String[] args = new String[]{"sh", "-c", command};
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(command); //è¿å¥è¯å°±æ¯shellä¸é«çº§è¯è¨é´çè°ç¨
//å¦ææåæ°çè¯å¯ä»¥ç¨å¦å¤ä¸ä¸ªè¢«éè½½çexecæ¹æ³
//å®é ä¸è¿æ ·æ§è¡æ¶å¯å¨äºä¸ä¸ªåè¿ç¨,å®æ²¡æç¶è¿ç¨çæ§å¶å°
//ä¹å°±çä¸å°è¾åº,æ以éè¦ç¨è¾åºæµæ¥å¾å°shellæ§è¡åçè¾åº
InputStream inputstream = proc.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
// read the ls output
String line = "";
StringBuilder sb = new StringBuilder(line);
while ((line = bufferedreader.readLine()) != null) {
//System.out.println(line);
sb.append(line);
sb.append('\n');
}
//tv.setText(sb.toString());
//使ç¨execæ§è¡ä¸ä¼çæ§è¡æå以åæè¿å,å®ä¼ç«å³è¿å
//æ以å¨æäºæ åµä¸æ¯å¾è¦å½ç(æ¯å¦å¤å¶æ件çæ¶å)
//使ç¨wairFor()å¯ä»¥çå¾ å½ä»¤æ§è¡å®æ以åæè¿å
try {
if (proc.waitFor() != 0) {
System.err.println("exit value = " + proc.exitValue());
}
}
catch (InterruptedException e) {
System.err.println(e);
}
}
}
2.ç´æ¥å®è£ shell模æå¨ï¼å³å·²ç»å¼å好çandroidåºç¨ï¼å¯å¨å类似windowsçdoså½ä»¤è¡ï¼å¯ä»¥ç´æ¥å®è£ 使ç¨ï¼å¯æ§è¡å¸¸ç¨çlinuxå½ä»¤ï¼åºç¨å¨é件ã
1.ç´æ¥å¨ä»£ç ä¸ç¨javaæä¾çRuntime è¿ä¸ªç±»æ¥æ§è¡å½ä»¤ï¼ä»¥ä¸ä¸ºå®æ´ç¤ºä¾ä»£ç ã
public void execCommand(String command) throws IOException {
// start the ls command running
//String[] args = new String[]{"sh", "-c", command};
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(command); //è¿å¥è¯å°±æ¯shellä¸é«çº§è¯è¨é´çè°ç¨
//å¦ææåæ°çè¯å¯ä»¥ç¨å¦å¤ä¸ä¸ªè¢«éè½½çexecæ¹æ³
//å®é ä¸è¿æ ·æ§è¡æ¶å¯å¨äºä¸ä¸ªåè¿ç¨,å®æ²¡æç¶è¿ç¨çæ§å¶å°
//ä¹å°±çä¸å°è¾åº,æ以éè¦ç¨è¾åºæµæ¥å¾å°shellæ§è¡åçè¾åº
InputStream inputstream = proc.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
// read the ls output
String line = "";
StringBuilder sb = new StringBuilder(line);
while ((line = bufferedreader.readLine()) != null) {
//System.out.println(line);
sb.append(line);
sb.append('\n');
}
//tv.setText(sb.toString());
//使ç¨execæ§è¡ä¸ä¼çæ§è¡æå以åæè¿å,å®ä¼ç«å³è¿å
//æ以å¨æäºæ åµä¸æ¯å¾è¦å½ç(æ¯å¦å¤å¶æ件çæ¶å)
//使ç¨wairFor()å¯ä»¥çå¾ å½ä»¤æ§è¡å®æ以åæè¿å
try {
if (proc.waitFor() != 0) {
System.err.println("exit value = " + proc.exitValue());
}
}
catch (InterruptedException e) {
System.err.println(e);
}
}
}
2.ç´æ¥å®è£ shell模æå¨ï¼å³å·²ç»å¼å好çandroidåºç¨ï¼å¯å¨å类似windowsçdoså½ä»¤è¡ï¼å¯ä»¥ç´æ¥å®è£ 使ç¨ï¼å¯æ§è¡å¸¸ç¨çlinuxå½ä»¤ï¼åºç¨å¨é件ã
温馨提示:答案为网友推荐,仅供参考