简体   繁体   中英

Android Thread: App hangs when screen turns off

I am using a Canvas in a SurfaceView Class which runs on a thread. The SurfaceView is called by an Activity (setContentView(surfaceview)). When the screen turns off, it goes through a pause() method which turns off all loop variables and so on.

The strange thing is: while on my tablet (Android 4.4.2) the Thread pauses and resumes correctly (the thread starts all over), however on my phone (Android 4.2.2) and on other people's phone (CM 11) the app hangs (it doesn't crash! it just hangs) and I have to kill it. LogCat doesn't print anything - however I tried to find out what happens using Log.i, it seems that after the pause method finishes, the pause method starts again, and this is where the app hangs.

I have checked before and the pause/resume methods are getting called correctly (override of onPause/onResume of the Activity using super.onPause()/Resume() and then surfaceview.pause()/resume())

My pause/resume methods:

public void resume() {
    countdown = true; // this one,
    threadRun = true;  // this one and
    finishWait = true; // this one each are loop controllers
    won = false;

    hitCount = 0;

    thread = new Thread(this);
    thread.start();
}

public void pause() {
    Log.i("codeflow", "Pause method start");

    // all loop controllers have been set to false
    countdown = false;
    threadRun = false;
    finishWait = false;

    /* Log.i("CodeFlow", "drawablank");
    drawABlankScreen(); // to hide everything drawn */

    while (true) {
        try {
            Log.i("codeflow", "Thread.join() loop");
            thread.join();
            break;
        } catch(InterruptedException e) {
            Log.i("codeflow", "InterruptedException");
            e.printStackTrace();
        }
    }
    thread = null;
    Log.i("codeflow", "Thread is killed");

    if (won && !lost) { // !lost IS necessary
        Log.i("CodeFlow", "save highscore");
        saveHighscore(highscore);
    }

}

The pause method goes through everything the way it should, but then it starts again (another "Pause method start") and goes either to drawABlankScreen() or, when this linke is commented, it prints "Thread.join() loop" once and then the app hangs.

The thing that bugs me the most it that it hangs on my phone while it doesn't hang on my tablet. Can anyone please help me, I've searched for hours and found nothing....

Joining the UI thread to a background thread in onPause() is probably what's causing your problem.

Instead, you should kill the thread.

" thread.join() " will hangs the thread which execute this sentence, until thread run over. Strangly, if execute in Android UI thread, Android would not crash.(sorry for my full of mistakes English).

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