Search code examples
javaandroidadmobinterstitial

Phone completely unresponsive after interstitial


So I'm trying to use some code from the youtube android 2D game tutorial. An admob interstitial comes up over my setContentView(game);. Sometimes when I close the test ad, my screen is frozen. Completely unresponsive. I can't even press the home button to exit.

I think there might be a problem in the thread, but it doesn't explain to me why the onTouchEvents aren't registering. If this can't be avoided, I'd at least like to give my user an opportunity exit. How do I solve this issue? What could cause the back and home buttons to stop working like this?

import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class MainThread extends Thread
{
    private int FPS = 30;
    private double averageFPS;
    private SurfaceHolder surfaceHolder;
    private GamePanel gamePanel;
    private boolean running;
    public static Canvas canvas;
public MainThread(SurfaceHolder surfaceHolder, GamePanel gamePanel)
{
    super();

    this.surfaceHolder = surfaceHolder;
    this.gamePanel = gamePanel;
}

@Override
public void run()
{
    long startTime;
    long timeMillis;
    long waitTime;
    long totalTime = 0;
    int frameCount =0;
    long targetTime = 1000/FPS;

    while(running) {
        startTime = System.nanoTime();
        canvas = null;

        //try locking the canvas for pixel editing
        try {
            canvas = this.surfaceHolder.lockCanvas();
            synchronized (surfaceHolder) {
                this.gamePanel.update();
                this.gamePanel.draw(canvas);
            }
        } catch (Exception e) {
        }
        finally{
            if(canvas!=null)
            {
                try {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
                catch(Exception e){e.printStackTrace();}
            }
        }

        timeMillis = (System.nanoTime() - startTime) / 1000000;
        waitTime = targetTime-timeMillis;

        try{
            this.sleep(waitTime);
        }catch(Exception e){}

        totalTime += System.nanoTime()-startTime;
        frameCount++;
        if(frameCount == FPS)
        {
            averageFPS = 1000/((totalTime/frameCount)/1000000);
            frameCount =0;
            totalTime = 0;
            System.out.println(averageFPS);
        }
    }
}
public void setRunning(boolean b)
{
    running=b;
}
}

Solution

  • I changed a few things and it works now. I honestly have no idea what happened, but I'm glad it's not happening now.