启用 StrictMode 时,它总是抱怨 FileReader 有 DiskReadViolation。无论我如何改进,它仍然存在。我怀疑它与 UIThread 滞后问题有关,请告诉我如何解决它,谢谢。
06-03 15:28:19.764: D/dalvikvm(5726): GC_CONCURRENT freed 1515K, 60% free 3122K/7751K, external 4429K/4959K, paused 19ms+14ms
06-03 15:28:19.803: D/StrictMode(5726): StrictMode policy violation; ~duration=216 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=23 violation=2
06-03 15:28:19.803: D/StrictMode(5726): at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:745)
06-03 15:28:19.803: D/StrictMode(5726): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:228)
06-03 15:28:19.803: D/StrictMode(5726): at java.io.FileInputStream.<init>(FileInputStream.java:80)
06-03 15:28:19.803: D/StrictMode(5726): at java.io.FileInputStream.<init>(FileInputStream.java:132)
06-03 15:28:19.803: D/StrictMode(5726): at java.io.FileReader.<init>(FileReader.java:66)
06-03 15:28:19.803: D/StrictMode(5726): at util.CpuUtil.getCpuFreq(CpuUtil.java:30)
public static float getCpuFreq() {
float result = -1;
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");// (CpuUtil.java:30)
br = new BufferedReader(fr);
result = Float.parseFloat(br.readLine().trim());
result = result / 1000;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fr != null) {
fr.close();
}
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
编辑:getCpuFreq() 被放入 asynctask,但 onpostexcute() 的位置错误。@rciovati,感谢您的提醒,请在答案中发表您的话,以便我接受。