首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从java发出android系统命令不能正常工作

从java发出android系统命令不能正常工作
EN

Stack Overflow用户
提问于 2018-05-25 23:50:36
回答 2查看 53关注 0票数 0

我目前需要从我的java代码中发出系统命令,但我坚持了很长时间。我已经尝试了很多论坛和许多代码片段,但是它们要么使应用程序崩溃,要么似乎对我上次尝试的代码没有做任何事情,虽然可以使用"echo“,但当我创建"echo anystuff > y.txt",而不是创建一个名为y.txt的文件并在文件中写入"anystuff”时,它只是在我创建的文本框中显示"anystuff > y.txt“,以查看我的方法

代码语言:javascript
复制
public void ShellTest() throws IOException {
    // test 10
    String cmd="echo anystuff > y.txt";
    StringBuffer cmdOut = new StringBuffer();
    Process process;
    try{
        // issue the command here
        process = Runtime.getRuntime().exec(cmd);

        // prepare to get back the result and put it in the textbox "resText"
        DataOutputStream stdin = new DataOutputStream(process.getOutputStream());
        stdin.writeBytes(cmd);

        InputStreamReader r = new InputStreamReader(process.getInputStream());
        BufferedReader bufReader = new BufferedReader(r);
        char[] buf = new char[4096];
        int nRead = 0;
        while ((nRead = bufReader.read(buf)) > 0){
            cmdOut.append(buf,0,nRead);
        }
        bufReader.close();
        try {
            process.waitFor();
        } catch (InterruptedException e){
            e.printStackTrace();
        }
    } catch (IOException e){
        e.printStackTrace();
    }
    // check by the showing the output of the command in the textbox
    resText.setText(cmdOut); 
} 

顺便说一句,我的手机是Google Pixel 2 XL

任何帮助都将不胜感激!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-30 08:40:25

引入SuperUser和SuperSU的Chainfire公司做了一个很棒的库来处理所有这些东西。安装这个库,它就可以正常工作了!

布局文件

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_gravity="center"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textAlignment="center"
        android:text="Shell Executer" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/input_text"
        android:hint="Example - ls" />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/execute_button"
        android:text="Execute" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/clear_button"
        android:text="Clear" />

    <TextView
        android:id="@+id/output_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:textAlignment="textStart" />

</LinearLayout >

Java文件

代码语言:javascript
复制
    public class MainActivity extends AppCompatActivity {

    Button execute, clear;
    EditText inputCommand;
    TextView outputResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        execute = findViewById(R.id.execute_button);
        clear = findViewById(R.id.clear_button);

        inputCommand = findViewById(R.id.input_text);
        outputResult = findViewById(R.id.output_text);
        outputResult.setMovementMethod(new ScrollingMovementMethod());

        execute.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                CommandResult result = Shell.SH.run(inputCommand.getText().toString());
                outputResult.append("$ " + inputCommand.getText().toString() + "\n" +
                        "> " + result.toString() + "\n");
            }
        });

        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                outputResult.setText("");
            }
        });
    }

}

结果:Image result

库链接及使用方法:https://github.com/jrummyapps/android-shell

工作测试项目:https://github.com/waleedhammam/Android-Shell

票数 0
EN

Stack Overflow用户

发布于 2018-05-25 23:53:25

因为重定向不是系统命令的一部分。这是一个(通常是bash) shell的特性。要让它工作,您需要运行一个shell,然后将命令作为输入提供给shell。单独将其作为命令运行会将其全部解释为命令回显的参数。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50532893

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档