2022-05-27 Android Android getRuntime()、exec 执行命令(linux 下的bin文件等)并解得到返回值

一、形式一:只执行命令不获取返回值。

    public static String execute_command(String cmd) {
        try {
            //String keyCommand = "setprop " + propName;
            Runtime runtime = Runtime.getRuntime();
            Process proc = runtime.exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  cmd;
    }

二、形式二:执行命令并且获取返回值。

   private String playRunTime(String cmd) throws Exception
   {
       //  String cmd = "adb version";
         String ret = null;
         Process p = Runtime.getRuntime().exec(cmd);
         InputStream is = p.getInputStream();
         BufferedReader reader = new BufferedReader(new InputStreamReader(is));
         String line; while ((line = reader.readLine()) != null)
         {
           //tv_result.append(line + "");
             ret = line;
             System.out.println(TAG+"  "+line);
         }
         p.waitFor();
         is.close();
         reader.close();
         p.destroy();
         return ret;
   }

三、实例测试

1、java源码

package com.giada.sn_writer;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
    private Button m_Button;
    private EditText m_EditText;
    private String TAG = "EXECUTECOMMAND";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        m_Button=(Button)findViewById(R.id.button);
        m_EditText = (EditText) findViewById(R.id.textView);

        m_Button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v)
            {
                String str = m_EditText.getText().toString();
              //  execute_command("sn_writer write-key "+ str);
              //  playRunTime("sn_writer write-key "+ str);
                try {
                    playRunTime("ls");
                    playRunTime("cat /sys/class/power_supply/battery/voltage_now");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }
    public static String execute_command(String cmd) {
        try {
            //String keyCommand = "setprop " + propName;
            Runtime runtime = Runtime.getRuntime();
            Process proc = runtime.exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  cmd;
    }

   private String playRunTime(String cmd) throws Exception
   {
       //  String cmd = "adb version";
         String ret = null;
         Process p = Runtime.getRuntime().exec(cmd);
         InputStream is = p.getInputStream();
         BufferedReader reader = new BufferedReader(new InputStreamReader(is));
         String line; while ((line = reader.readLine()) != null)
         {
           //tv_result.append(line + "");
             ret = line;
             System.out.println(TAG+"  "+line);
         }
         p.waitFor();
         is.close();
         reader.close();
         p.destroy();
         return ret;
   }
}

2、执行效果

猜你喜欢

转载自blog.csdn.net/qq_37858386/article/details/125002811