1

我需要从本地方法 in 制作一个 linux 命令C++,即iwconfig,也popen用于获取该命令的输出。

这是我的代码

std::string exec(const char* cmd) {
FILE* pipe = popen(cmd, "r");
if (!pipe) return "ERROR";
char buffer[128];
std::string result = "";
while(!feof(pipe)) {
  if(fgets(buffer, 128, pipe) != NULL)
  result += buffer;
  }
  pclose(pipe);
  return result;
 }

我想知道是否可以调用它!

所以我在这个链接中有这个代码

4

1 回答 1

0

将 'iwconfig' 放在 shell 脚本中,您可以直接从 android 运行进程,处理 stdout 直到完成。此示例执行 shell name=./bin/pars_submit。

        public String getFfmpeg(@QueryParam("infil1") String infil1, 
                @QueryParam("infil2") String infil2, @QueryParam("otfil") String otfil,
                @QueryParam("t") String time) {         
        String outfil = "dummy.mp4";

          List<String> command = new ArrayList<String>();
            command.add("vendor/bin/pars_submit");

            command.add(infil1);     

            command.add(infil2);
            command.add(otfil);
            command.add(time);

System.out.println("Starting process " +command.toString());
            ProcessBuilder builder = new ProcessBuilder(command);
            Map<String, String> environ = builder.environment();
//          for(Entry<String, String> entry : environ.entrySet()){
    //          System.out.println("ENV " +entry.getKey() + " " +entry.getValue());
      //      }
//          builder.redirectErrorStream(true);
            Process process = null;
            try {
                process = builder.start();

            InputStream is = process.getInputStream();

            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line;
            while ((line = br.readLine()) != null) {  
              //System.out.println(line);
                outfil=line;
            }

//          int exitVal = process.waitFor();
 //           System.out.println("Process exitValue: " + exitVal);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
            finally {
                  if (process != null) {
                    process.destroy();
                    process = null;
                  }


                }           


            return outfil;


                }
于 2013-03-06T01:29:46.967 回答