简体   繁体   中英

How to execute terminal commands programmatically in android

在我的应用程序中,我想执行Linux终端命令,例如“ ls”,并在EditText中显示结果。

Try this

    try
    {
        Process process = Runtime.getRuntime().exec("ls");
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));

        StringBuilder result = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null)
        {
            result.append(line + "\n");
        }
        EditText et = (EditText) findViewById(R.id.edittext);
        et.setText(result.toString());
    }
    catch (IOException e)
    {
        throw new RuntimeException(e);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM