From: kfl_614 Date: Wed, 16 Dec 2020 08:51:06 +0000 (+0800) Subject: switch shell to file reade. X-Git-Url: http://git.snicker.com.cn/Users/apple/Desktop/static/gitweb.css?a=commitdiff_plain;h=cd79eee8751f330f8bef38a6db4147d33b2fa10b;p=icworkservice.git switch shell to file reade. --- diff --git a/app/src/main/java/com/ic/work/utils/ShellUtil.java b/app/src/main/java/com/ic/work/utils/ShellUtil.java deleted file mode 100644 index 9c3b1ee..0000000 --- a/app/src/main/java/com/ic/work/utils/ShellUtil.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.ic.work.utils; - -import android.util.Log; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -public class ShellUtil { - /** - * 执行命令并且输出结果 - */ - public static String execRootCmd(String cmd) { - String result = ""; - DataOutputStream dos = null; - DataInputStream dis = null; - - try { - Process p = Runtime.getRuntime().exec(cmd);// 经过Root处理的android系统即有su命令 - dos = new DataOutputStream(p.getOutputStream()); - dis = new DataInputStream(p.getInputStream()); - String line; - while ((line = dis.readLine()) != null) { - Log.d("result", line); - result += line; - } - p.waitFor(); - } catch (Exception e) { - e.printStackTrace(); - } finally { - if (dos != null) { - try { - dos.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - if (dis != null) { - try { - dis.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - return result; - } -} diff --git a/app/src/main/java/com/ic/work/utils/TempFileUtils.java b/app/src/main/java/com/ic/work/utils/TempFileUtils.java new file mode 100644 index 0000000..3e1f5a7 --- /dev/null +++ b/app/src/main/java/com/ic/work/utils/TempFileUtils.java @@ -0,0 +1,38 @@ +package com.ic.work.utils; + +import android.text.TextUtils; + +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.InputStreamReader; + +public class TempFileUtils { + /** + * 执行命令并且输出结果 + */ + public static String readFile(String file) { + if (TextUtils.isEmpty(file)) { + return null; + } + + BufferedReader bf = null; + try { + FileInputStream fis = new FileInputStream(file); + InputStreamReader isr = new InputStreamReader(fis); + bf = new BufferedReader(isr); + String result = bf.readLine(); + return result; + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (bf != null) { + try { + bf.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + return null; + } +} diff --git a/app/src/main/java/com/ic/work/utils/TemperatureUtil.java b/app/src/main/java/com/ic/work/utils/TemperatureUtil.java index 0d4aedf..126991c 100644 --- a/app/src/main/java/com/ic/work/utils/TemperatureUtil.java +++ b/app/src/main/java/com/ic/work/utils/TemperatureUtil.java @@ -53,20 +53,20 @@ public class TemperatureUtil { //thermal_zone0 - public static final String CMD_CPU_TEM = "cat /sys/class/thermal/thermal_zone0/temp"; - public static final String CMD_CPU_TYPE = "cat /sys/class/thermal/thermal_zone0/type"; + public static final String CMD_CPU_TEM = "/sys/class/thermal/thermal_zone0/temp"; + public static final String CMD_CPU_TYPE = "/sys/class/thermal/thermal_zone0/type"; //thermal_zone1 - public static final String CMD_GPU_TEM = "cat /sys/class/thermal/thermal_zone1/temp"; - public static final String CMD_GPU_TYPE = "cat /sys/class/thermal/thermal_zone1/type"; + public static final String CMD_GPU_TEM = "/sys/class/thermal/thermal_zone1/temp"; + public static final String CMD_GPU_TYPE = "/sys/class/thermal/thermal_zone1/type"; //thermal_zone2 - public static final String CMD_CHARGE_TEM = "cat /sys/class/thermal/thermal_zone2/temp"; - public static final String CMD_CHARGE_TYPE = "cat /sys/class/thermal/thermal_zone2/type"; + public static final String CMD_CHARGE_TEM = "/sys/class/thermal/thermal_zone2/temp"; + public static final String CMD_CHARGE_TYPE = "/sys/class/thermal/thermal_zone2/type"; //thermal_zone3 - public static final String CMD_PA_TEM = "cat /sys/class/thermal/thermal_zone3/temp"; - public static final String CMD_PA_TYPE = "cat /sys/class/thermal/thermal_zone3/type"; + public static final String CMD_PA_TEM = "/sys/class/thermal/thermal_zone3/temp"; + public static final String CMD_PA_TYPE = "/sys/class/thermal/thermal_zone3/type"; //thermal_zone4 - public static final String CMD_BOARD_TEM = "cat /sys/class/thermal/thermal_zone4/temp"; - public static final String CMD_BOARD_TYPE = "cat /sys/class/thermal/thermal_zone4/type"; + public static final String CMD_BOARD_TEM = "/sys/class/thermal/thermal_zone4/temp"; + public static final String CMD_BOARD_TYPE = "/sys/class/thermal/thermal_zone4/type"; /** @@ -83,16 +83,16 @@ public class TemperatureUtil { double paTemp/*射频的温度*/ = 0.0f; double boardTemp/*主板的温度*/ = 0.0f; try { - String cpuTempType = ShellUtil.execRootCmd(CMD_CPU_TYPE); - String cpuTempStr = ShellUtil.execRootCmd(CMD_CPU_TEM); - String gpuTempType = ShellUtil.execRootCmd(CMD_GPU_TYPE); - String gpuTempStr = ShellUtil.execRootCmd(CMD_GPU_TEM); - String chargeTempType = ShellUtil.execRootCmd(CMD_CHARGE_TYPE); - String chargeTempStr = ShellUtil.execRootCmd(CMD_CHARGE_TEM); - String paTempType = ShellUtil.execRootCmd(CMD_PA_TYPE); - String paTempStr = ShellUtil.execRootCmd(CMD_PA_TEM); - String boardTempType = ShellUtil.execRootCmd(CMD_BOARD_TYPE); - String boardTempStr = ShellUtil.execRootCmd(CMD_BOARD_TEM); + String cpuTempType = TempFileUtils.readFile(CMD_CPU_TYPE); + String cpuTempStr = TempFileUtils.readFile(CMD_CPU_TEM); + String gpuTempType = TempFileUtils.readFile(CMD_GPU_TYPE); + String gpuTempStr = TempFileUtils.readFile(CMD_GPU_TEM); + String chargeTempType = TempFileUtils.readFile(CMD_CHARGE_TYPE); + String chargeTempStr = TempFileUtils.readFile(CMD_CHARGE_TEM); + String paTempType = TempFileUtils.readFile(CMD_PA_TYPE); + String paTempStr = TempFileUtils.readFile(CMD_PA_TEM); + String boardTempType = TempFileUtils.readFile(CMD_BOARD_TYPE); + String boardTempStr = TempFileUtils.readFile(CMD_BOARD_TEM); //cpuTempType == > cpu-thmzone cpuTempStr == > 45295 gpuTempType == > gpu-thmzone gpuTempStr == > 43910 Log.d(TAG, " cpuTempType == > " + cpuTempType + " cpuTempStr == > " + cpuTempStr +