Android:执行 onPause 后按返回(和方向改变)会导致挂起

发布于 2024-11-24 09:16:52 字数 1733 浏览 0 评论 0原文

因此,我设法让我的程序在按下主页按钮时失去焦点,并在恢复时成功恢复活动,不幸的是,我使用的方法会导致线程在屏幕发生变化时挂起,而不会失去焦点立即地。

编辑--将 onResume 放入主 Activity 中

@Override
protected void onPause()
{
    super.onPause();
    synchronized (mPauseLock) {
        mPaused = true;
    }
    return;
}
@Override
public void onResume() {
    super.onResume();
    synchronized (mPauseLock) {
        mPaused = false;
        mPauseLock.notifyAll();
    }
}

public class ThreadClass extends Thread
{
    boolean isRunning;
    public void BeginThread()
    {

        if (mGameIsRunning) {
            onResume();
        }else{
            if(isRunning)return;
            isRunning=true;
            start();
            mGameIsRunning=true;
        }
    }



    public void run()
    {
        Canvas canvas;
        while(isRunning)
        {
            canvas=null;
            try
            {
                canvas = canvasClass.surfaceHolder.lockCanvas(null);
                synchronized (canvasClass.surfaceHolder)
                {
                    //processClass.processGame();
                    canvasClass.onDraw(canvas);
                }
            }finally{
                if(canvas!=null)
                {
                    canvasClass.surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
            synchronized (mPauseLock)
            {
                while (mPaused)
                {
                    try
                    {
                        mPauseLock.wait();
                    } catch (InterruptedException e) {

                    }
                }
            }
        }           
    }
}

必须更改什么才能使 Activity 在按 Home 时暂停并在按 Back 时被抛出堆栈?另外,正如所解释的,当屏幕方向改变时也会发生同样的问题。

So I manage to get my program to lose focus when pressing the Home button and successfully bring the activity back when resuming, unfortunately the method I'm using causes the Thread to hang when the screen changes in a matter that it doesn't lose focus immediately.

EDITTED -- Placed onResume into main Activity

@Override
protected void onPause()
{
    super.onPause();
    synchronized (mPauseLock) {
        mPaused = true;
    }
    return;
}
@Override
public void onResume() {
    super.onResume();
    synchronized (mPauseLock) {
        mPaused = false;
        mPauseLock.notifyAll();
    }
}

public class ThreadClass extends Thread
{
    boolean isRunning;
    public void BeginThread()
    {

        if (mGameIsRunning) {
            onResume();
        }else{
            if(isRunning)return;
            isRunning=true;
            start();
            mGameIsRunning=true;
        }
    }



    public void run()
    {
        Canvas canvas;
        while(isRunning)
        {
            canvas=null;
            try
            {
                canvas = canvasClass.surfaceHolder.lockCanvas(null);
                synchronized (canvasClass.surfaceHolder)
                {
                    //processClass.processGame();
                    canvasClass.onDraw(canvas);
                }
            }finally{
                if(canvas!=null)
                {
                    canvasClass.surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
            synchronized (mPauseLock)
            {
                while (mPaused)
                {
                    try
                    {
                        mPauseLock.wait();
                    } catch (InterruptedException e) {

                    }
                }
            }
        }           
    }
}

What has to be changed so that the activity will pause when pressing Home and be thrown out of the stack when pressing Back? Also as explained, the same issue happens when screen orientation changes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

风铃鹿 2024-12-01 09:16:52

为什么 onResume 位于线程类内部?
如果这是实际代码,请将其删除到活动类中。
其余代码看起来不错。

Why does the onResume is inside the thread class?
If that's the actual code, remove it to the activity class.
Rest of the code looks fine.

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