建立一个终端仿真器为Android [英] Building a Terminal Emulator for Android

查看:170
本文介绍了建立一个终端仿真器为Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图建立Android的终端模拟器。作为pretty新来这个,我的想法是要执行的每个命令和输出存储在一个文件,其内容将显示在每个执行之后。
伪code:

I've been trying to build a Terminal Emulator for Android. Being pretty new to this, my idea was to execute each command and store the output in a file, whose contents would be displayed after each execution. Pseudo Code :

public Boolean execCommands(String command) {
        try {
            rt = Runtime.getRuntime();
            process = rt.exec("su");
            DataOutputStream os = new DataOutputStream(process.getOutputStream());
            os.writeBytes("echo $ \""+command+ "\" >> /sdcard/Android/data/terminalemulatorlog.txt\n\n\n"); 
            /**** Note :  String command = (EditText)findViewById(R.id.command).getText().toString(); ****/
            os.flush();
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
            }
        // Error Handling
        displayOutput(); //Loads and displays the Text File (/sdcard/Android/data/terminalemulatorlog.txt)
        return true;
        }

这件code的工作除了一些特殊命令(如清晰的)。
但我更关注的是以下问题:

This piece of code works except for a few special commands (Eg. 'clear'). But what I'm more concerned about are the following problems :


  1. 每次命令要执行,我最终寻找超级用户权限(code的第二行)。我想这个做了。

  2. 在情况下,当用户输入一个命令接踵而至,结果
    如:

  1. Each time a command is to be executed, I end up seeking SuperUser permissions (second line of code). And I'd like to do away with this.
  2. In cases when the user enters one command followed by another,
    Such as :

cd /sdcard    
touch File.txt    

FILE.TXT 的是'/'创建,而不是'/ SD卡。截至目前,以避免这种情况,我将所有的CD的轨道命令找出present工作目录是什么。而且我希望有解决这个更好的方法。

The File.txt is created in '/' and not in '/sdcard'. As of now to avoid this, I'm keeping a track of all the 'cd' commands to figure out what the present working directory is. And I'm hoping that there is a better way around this.

我会很感激,如果有人可以帮助我在这里。

I'd be grateful if someone could help me out here.

推荐答案

不知道如果你仍然需要这个与否,但在这里是如何我在同一时间发出多个命令,而不是使用苏让他们跑。

Not sure if you are still needing this or not, but here is how I am issuing multiple commands at one time and not using "su" to have them run.

try {
            String[] commands = {

                    "dumpstate > /sdcard/LogFiles/dumpstate.txt",
                    "dumpsys > /sdcard/LogFiles/dumpsys.txt",
                    "logcat -d > /sdcard/LogFiles/log.txt",
                    "cat /sdcard/LogFiles/dumpstate.txt /sdcard/LogFiles/dumpsys.txt /sdcard/LogFiles/log.txt > /sdcard/LogFiles/bugreport.rtf" };
            Process p = Runtime.getRuntime().exec("/system/bin/sh -");
            DataOutputStream os = new DataOutputStream(p.getOutputStream());
            for (String tmpCmd : commands) {
                os.writeBytes(tmpCmd + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

这篇关于建立一个终端仿真器为Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆