How to read output of android process command

后端 未结 1 571

I am trying to get the output of android shell command \'getprop\' with java since getprop() always returns null no matter what.

I tried this from developer.android.com:

1条回答
  •  忘了有多久
    2021-02-04 09:50

    Is there any particular reason why you want to run the command as an external process? There is a simpler way:

    String android_rel_version = android.os.Build.VERSION.RELEASE;
    

    However, if you really want to do it via a shell command, here is the way I got it to work:

    try {
          // Run the command
          Process process = Runtime.getRuntime().exec("getprop");
          BufferedReader bufferedReader = new BufferedReader(
                  new InputStreamReader(process.getInputStream()));
    
          // Grab the results
          StringBuilder log = new StringBuilder();
          String line;
          while ((line = bufferedReader.readLine()) != null) {
              log.append(line + "\n");
          }
    
          // Update the view
          TextView tv = (TextView)findViewById(R.id.my_text_view);
          tv.setText(log.toString());
    } catch (IOException e) {
    }
    

    0 讨论(0)
提交回复
热议问题