android 代碼中調用shell

1. 新建自己的shell文件,或者中windows上傳

    例如:   d://x.sh

  1. #! /system/bin/sh
  2. #最簡單的一個shell,沒什麼功能
  3. #需要注意的是必須是 #! /system/bin/sh開頭(有空格)
  4. #和一般linux不同,如果用#!/bin/sh就會找不到(因爲沒有)
  5. ls

複製代碼在cmd中用adb push d://x.sh /data/kenshin上傳shell

    第一個參數是本地文件位置,第二個是遠程位置(也就是放在android中的位置),在根目錄的data目錄下,kenshin是我自己創建的一個文件夾.  如果失敗了,說是隻有隻讀權限的話,需要修改文件夾的權限.具體如下:

    進入adb shell,使用mount命令查看掛了哪些塊.

  1. C:/Documents and Settings/Kenshintang>adb shell
  2. # mount
  3. mount
  4. rootfs / rootfs ro 0 0
  5. tmpfs /dev tmpfs rw,mode=755 0 0
  6. devpts /dev/pts devpts rw,mode=600 0 0
  7. proc /proc proc rw 0 0
  8. sysfs /sys sysfs rw 0 0
  9. tmpfs /sqlite_stmt_journals tmpfs rw,size=4096k 0 0
  10. /dev/block/mtdblock0 /system yaffs2 ro,noatime,nodiratime 0 0
  11. /dev/block/mtdblock1 /data yaffs2 rw,nosuid,nodev 0 0
  12. /dev/block/mtdblock2 /cache yaffs2 rw,nosuid,nodev 0 0

複製代碼比如/dev/block/mtdblock0 /system就是隻讀的,/dev/block/mtdblock1 /data是讀寫的
    修改讀寫權限用mount命令
    例如  mount -t yaffs2 -o remount,rw,noatime,nodiratime /dev/mtdblock0 /system
    -o選項用來描述設備或者檔案的掛接方式,常用的有
           loop:用來把一個文件當成硬盤分區掛接上系統
           ro:採用只讀方式掛接設備
           rw:採用讀寫方式掛接設備

     具體的用法可以google linux命令mout的用法.
      上面那句話的意思就是讓/system文件夾有讀寫的權限

2. cd到shell所在的文件夾,查看x.sh的權限

一般上傳上去的話是沒有執行權限的,可以用ls -l查看
    使用chmod 777 x.sh增加權限,具體可以google chmod的用法.

3. 執行   ./x.sh

4. 在程序中執行shell package com.kenshin.hellocommand;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

public class HelloCommand extends Activity {

    /** Called when the activity is first created. */

        private TextView tv;

        

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        tv = (TextView)findViewById(R.id.tv01);

        

        

        try {

                execCommand("./data/kenshin/x.sh");   

                } catch (IOException e) {

                        // TODO Auto-generated catch block

                        e.printStackTrace();

                }

    }

   

    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);

        }

    }

}


another way to create

  1. /** 
  2. * 執行一個shell命令,並返回字符串值 
  3. * 
  4. * @param cmd 
  5. * 命令名稱&參數組成的數組(例如:{"/system/bin/cat", "/proc/version"}) 
  6. * @param workdirectory 
  7. * 命令執行路徑(例如:"system/bin/") 
  8. * @return 執行結果組成的字符串 
  9. * @throws IOException 
  10. */ 
  11. public static synchronized String run(String[] cmd, String workdirectory) 
  12.         throws IOException { 
  13.     StringBuffer result = new StringBuffer(); 
  14.     try { 
  15.         // 創建操作系統進程(也可以由Runtime.exec()啓動) 
  16.         // Runtime runtime = Runtime.getRuntime(); 
  17.         // Process proc = runtime.exec(cmd); 
  18.         // InputStream inputstream = proc.getInputStream(); 
  19.         ProcessBuilder builder = new ProcessBuilder(cmd); 
  20.  
  21.         InputStream in = null
  22.         // 設置一個路徑(絕對路徑了就不一定需要) 
  23.         if (workdirectory != null) { 
  24.             // 設置工作目錄(同上) 
  25.             builder.directory(new File(workdirectory)); 
  26.             // 合併標準錯誤和標準輸出 
  27.             builder.redirectErrorStream(true); 
  28.             // 啓動一個新進程 
  29.             Process process = builder.start(); 
  30.  
  31.             // 讀取進程標準輸出流 
  32.             in = process.getInputStream(); 
  33.             byte[] re = new byte[1024]; 
  34.             while (in.read(re) != -1) { 
  35.                 result = result.append(new String(re)); 
  36.             } 
  37.         } 
  38.         // 關閉輸入流 
  39.         if (in != null) { 
  40.             in.close(); 
  41.         } 
  42.     } catch (Exception ex) { 
  43.         ex.printStackTrace(); 
  44.     } 
  45.     return result.toString(); 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章