gpt4 book ai didi

Android StrictMode 总是提示 FileReader

转载 作者:行者123 更新时间:2023-11-30 03:34:14 31 4
gpt4 key购买 nike

当启用 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,谢谢你的提醒,请在答案中发表你的话,这样我就可以接受了。

最佳答案

考虑使用 AsyncTask:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new YourAsyncTask().execute();
}

static class YourAsyncTask extends AsyncTask<Void, Void, String> {

@Override
protected String doInBackground(Void... unused) {
//Put your code inside here
}
}
}

关于Android StrictMode 总是提示 FileReader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16892364/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com