如何降低Android手机App的CPU使用率?
我开发了一个自动呼叫应用程序。该应用程序读取包含电话号码列表的文本文件,并拨打几秒钟,结束通话,然后重复。
我的问题是该应用程序在 10~16 小时后不发送呼叫。我不知道具体原因,但我猜测是CPU使用率的问题。我的应用程序的 CPU 使用率几乎是 50%!如何减少 CPU 使用率?
以下是部分源代码:
if(r_count.compareTo("0")!=0) {
while(index < repeat_count) {
count = 1;
time_count = 2;
while(count < map.length) {
performDial(); //start call
reject(); //end call
finishActivity(1);
TimeDelay("60"); // wait for 60sec
count = count + 2;
time_count = time_count + 2;
onBackPressed(); // press back button for calling next number
showCallLog();
finishActivity(0);
}
index++;
}
这是 TimeDelay()
方法源代码:
public void TimeDelay(String delayTime) {
saveTime = System.currentTimeMillis()/1000;
currentTime = 0;
dTime = Integer.parseInt(delayTime);
while(currentTime - saveTime < dTime) {
currentTime = System.currentTimeMillis()/1000;
}
}
TimeDelay()
在 while 循环中重复几次。
I developed an auto-call application. The app reads a text file that includes a phone number list and calls for a few second, ends the call and then repeats.
My problem is that the app does not send calls after 10~16 hours. I don't know the reason exactly, but I guess that the problem is the CPU usage. My app's CPU usage is almost 50%! How do I reduce CPU usage?
Here is part of source code:
if(r_count.compareTo("0")!=0) {
while(index < repeat_count) {
count = 1;
time_count = 2;
while(count < map.length) {
performDial(); //start call
reject(); //end call
finishActivity(1);
TimeDelay("60"); // wait for 60sec
count = count + 2;
time_count = time_count + 2;
onBackPressed(); // press back button for calling next number
showCallLog();
finishActivity(0);
}
index++;
}
This is the TimeDelay()
method source:
public void TimeDelay(String delayTime) {
saveTime = System.currentTimeMillis()/1000;
currentTime = 0;
dTime = Integer.parseInt(delayTime);
while(currentTime - saveTime < dTime) {
currentTime = System.currentTimeMillis()/1000;
}
}
TimeDelay()
repeats in the while loop for a few times.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它使用 50% 的 CPU 的原因是 Android 显然不会让它使用 100% 的 CPU,而像
TimeDelay()
中的循环通常会这样做。 (否则你有两个 CPU,并且实际上 100% 使用了一个 CPU。)你所做的称为 忙等待,并且很明显为什么不断检查条件会使用大量 CPU。所以不要这样做。请改用 Thread.sleep() 。然后,您的应用程序在等待期间将完全不使用 CPU。另外,看在上帝的份上,为什么要传递一个字符串,然后对其进行解析,而不是首先传递一个整数? :-)
The reason it's using 50% of your CPU is that Android apparently won't let it use 100% of the CPU, which a loop like the one in your
TimeDelay()
ordinarily would. (Or else you have two CPUs and it is in fact using 100% of one CPU.) What you're doing is called a busy wait and it should be obvious why continually checking a condition will use lots of CPU. So don't do that. UseThread.sleep()
instead. Your app will then use no CPU at all during the wait.Also, for God's sake, why are you passing a string and then
parseInt
ing it, rather than just passing anInteger
in the first place? :-)如果你的方法需要很长时间才能完成,尤其是 while 循环。您应该将 Thread.sleep(50) 放入循环内。这使您的处理器能够处理其他进程。
你的CPU将会减少。不确定,但你应该尝试一下。
希望你能得到好的结果。
If your method take a long time to finish , especially the while loop. You should put
Thread.sleep(50)
inside your loop. This makes you processor be able to handle other processes.Your CPU will be reduced. Not sure but you should try.
Hope you get good result.