我使用以下代码获取cpu percetange(类似于62.5465%),但似乎需要很长时间才能刷新/更新,除非您手动导航到另一个活动然后返回。有没有办法强制信息每秒更新一次?任何一个人都会受到赞赏。
private String ReadCPUinfo()
{
ProcessBuilder cmd;
String result="";
try{
String[] args = {"/system/bin/cat", "/proc/cpuinfo"};
cmd = new ProcessBuilder(args);
Process process = cmd.start();
InputStream in = process.getInputStream();
byte[] re = new byte[1024];
while(in.read(re) != -1){
System.out.println(new String(re));
result = result + new String(re);
}
in.close();
} catch(IOException ex){
ex.printStackTrace();
}
return result;
}
用于在TextView中显示文本。
device.setText(Html.fromHtml(
"CPU Speed: " + readUsage() * 100
));
答案 0 :(得分:4)
Timer
和TimerTask
可以使用。
Timer timer = new Timer();
MyTimerTask myTimerTask = new MyTimerTask();
timer.schedule(myTimerTask, 0, 1000); // 0 Delay, Repeat after 1 second
您的MyTimerTask
应该是这样的
class MyTimerTask extends TimerTask
{
public void run()
{
runOnUiThread(new Runnable()
{
@Override
public void run()
{
device.setText(Html.fromHtml("CPU Speed: " + readUsage() * 100));
}
}
}
}
修改强>
您也可以使用此方法
device.postDelayed(new Runnable()
{
@Override
public void run()
{
device.setText(Html.fromHtml("CPU Speed: " + readUsage() * 100));
}
}, 1000);