如何降低Android手机App的CPU使用率?

发布于 2024-11-12 22:33:04 字数 1145 浏览 5 评论 0原文

我开发了一个自动呼叫应用程序。该应用程序读取包含电话号码列表的文本文件,并拨打几秒钟,结束通话,然后重复。

我的问题是该应用程序在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薔薇婲 2024-11-19 22:33:04

它使用 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. Use Thread.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 parseInting it, rather than just passing an Integer in the first place? :-)

白况 2024-11-19 22:33:04

如果你的方法需要很长时间才能完成,尤其是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文