BaseUtils.java 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package com.emato.ich.utils;
  2. import android.util.Log;
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. public class BaseUtils {
  8. public static String getMac() {
  9. // start the ls command running
  10. //String[] args = new String[]{"sh", "-c", command};
  11. Runtime runtime = Runtime.getRuntime();
  12. String command = "cat /sys/class/net/eth0/address";
  13. Process proc = null; //这句话就是shell与高级语言间的调用
  14. try {
  15. proc = runtime.exec(command);
  16. //如果有参数的话可以用另外一个被重载的exec方法
  17. //实际上这样执行时启动了一个子进程,它没有父进程的控制台
  18. //也就看不到输出,所以我们需要用输出流来得到shell执行后的输出
  19. InputStream inputstream = proc.getInputStream();
  20. InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
  21. BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
  22. // read the ls output
  23. String line = "";
  24. StringBuilder sb = new StringBuilder(line);
  25. while ((line = bufferedreader.readLine()) != null) {
  26. //System.out.println(line);
  27. sb.append(line);
  28. sb.append('\n');
  29. }
  30. return sb.toString();
  31. } catch (IOException e) {
  32. return null;
  33. }
  34. //tv.setText(sb.toString());
  35. //使用exec执行不会等执行成功以后才返回,它会立即返回
  36. //所以在某些情况下是很要命的(比如复制文件的时候)
  37. //使用wairFor()可以等待命令执行完成以后才返回
  38. // try {
  39. // if (proc.waitFor() != 0) {
  40. // System.err.println("exit value = " + proc.exitValue());
  41. // }
  42. // } catch (InterruptedException e) {
  43. // System.err.println(e);
  44. // }
  45. // return null;
  46. }
  47. }