代码之家  ›  专栏  ›  技术社区  ›  Shouvik

有没有办法在android上以编程方式运行shell命令?

  •  37
  • Shouvik  · 技术社区  · 14 年前

    有没有办法在我的应用程序上运行终端命令,然后访问UI上的数据?明确地 top .

    4 回复  |  直到 14 年前
        1
  •  37
  •   EboMike    12 年前

    以日志收集器为例。这是你的名字 relevant file .

    关键在于:

    ArrayList<String> commandLine = new ArrayList<String>();
    commandLine.add("logcat");//$NON-NLS-1$
    [...]
    
    Process process = Runtime.getRuntime().exec(commandLine);
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    
        2
  •  20
  •   ademar111190    11 年前

    好吧,这正是我想要的,以防将来有人需要……:)

    围着试试看

    try {
        Process process = Runtime.getRuntime().exec("top -n 1 -d 1");
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    
        3
  •  9
  •   Kirk Woll    13 年前

    我们,可以执行如下命令,我成功地做到了。。。。!像这样尝试,这里我们需要指定命令的完整路径。要获取命令的完整路径,请在ur terminal(android)键入

    *$哪个ls

    try {
    
        // Executes the command.
    
        Process process = Runtime.getRuntime().exec("/system/bin/ls /sdcard");
    
        // Reads stdout.
        // NOTE: You can write to stdin of the command using
        //       process.getOutputStream().
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));
        int read;
        char[] buffer = new char[4096];
        StringBuffer output = new StringBuffer();
        while ((read = reader.read(buffer)) > 0) {
            output.append(buffer, 0, read);
        }
        reader.close();
    
        // Waits for the command to finish.
        process.waitFor();
    
        return output.toString();
    } catch (IOException e) {
    
        throw new RuntimeException(e);
    
    } catch (InterruptedException e) {
    
        throw new RuntimeException(e);
    }
    
        4
  •  5
  •   androidworkz    14 年前

    这也取决于你在终端运行什么。。。如果你在一个文件上运行“cat”,你也可以这样做。

    final private String MEM_FILE = "/proc/meminfo";
    
    public Long readMem() {
        String[] segs;
        FileReader fstream;
        try {
            fstream = new FileReader(MEM_FILE);
        } catch (FileNotFoundException e) {
            Log.e("readMem", "Could not read " + MEM_FILE);
            return false;
        }
        BufferedReader in = new BufferedReader(fstream, 500);
        String line;
        try {
            while ((line = in.readLine()) != null) {
                if (line.indexOf("MemTotal:") > 0) {
                    Log.e("MemTotal", line);
                    segs = line.trim().split("[ ]+");
                    memTotal = Long.parseLong(segs[1]);
                }
                if (line.indexOf("MemFree:") > 0) {
                    Log.e("MemFree", line);
                    segs = line.trim().split("[ ]+");
                    memFree = Long.parseLong(segs[1]);
                }
            }
            updateMem(); //call function to update textviews or whatever
            return true;
        } catch (IOException e) {
            Log.e("readMem", e.toString());
        }
        return false;
    }
    

    编辑: 在android实验室的netmeter项目中有一个很好的例子。有一个名为Top.java的类,它实际执行您想要的操作,并在TaskList.java中使用它来显示。 http://code.google.com/p/android-labs/source/browse/#svn/trunk/NetMeter/src/com/google/android/netmeter

        5
  •  0
  •   Phantom    3 年前

    fun executeShell() {
        val command: String = "top -n 1"
        try {
            val process: Process = Runtime.getRuntime().exec(command)
            // Read the lines using BufferedReader
            BufferedReader(InputStreamReader(process.inputStream)).forEachLine {
                // Do something on each line read
                Log.d(this::class.java.canonicalName, "$it")
            }
        } catch (e: InterruptedException) {
            Log.w(this::class.java.canonicalName, "Cannot execute command [$command].", e)
        } catch (e: Exception) {
            Log.e(this::class.java.canonicalName, "Cannot execute command [$command].", e)
        }
    }
    

    你甚至不需要关闭缓冲区,就像 扩展函数处理它。