栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

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

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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、执行效果

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/941838.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号