简体   繁体   中英

Using Thread.sleep() for pausing and resuming app

Hi,
In my app im trying to use Thread.sleep(100) to pause my thread, while its backgrounded in order to use less cpu, but it freezes when I open it back up.
I realized that onResume is not being called when I reopen the app.

Any ideas why?

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");              
}     

When you put your thread to sleep you are also blocking the UI thread thus leading to freezing of your application.

You need to check the Threads in Android way for the best performance.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM