使用Thread.sleep()暂停和恢复应用程序

时间:2012-05-05 19:46:00

标签: android android-lifecycle

您好,
在我的应用程序中我试图使用Thread.sleep(100)暂停我的线程,而它的后台为了使用更少的CPU,但它在我打开它时冻结。
我意识到当我重新打开应用程序时,onResume没有被调用。

任何想法为什么?

public void onPause() {
        pause = true;
        Log.d("mSTATE","THREADPAUSE");
    }

public void onResume() {
    pause = false;
        running = true;
       Log.d("mSTATE","THREADRESUME");
    }
public void run() {

    while(running){
         while(pause && running){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
         }

         while (!pause && running) {
             Canvas c = null;
             try {
                 c = sHolder.lockCanvas(null);
                 synchronized (sHolder) {
                     doDraw(c);
                     powerUps();
                 }
             } finally {
                 if (c != null) {
                     sHolder.unlockCanvasAndPost(c);
                 }
             }
         }
    }
         Log.d("mState","EndofRun");              
}     

1 个答案:

答案 0 :(得分:2)

当您将线程置于睡眠状态时,您也会阻止UI线程,从而导致应用程序冻结。

您需要以Android方式检查主题,以获得最佳效果。

http://developer.android.com/resources/articles/painless-threading.html

相关问题