首页 > 解决方案 > Getting ANR when GLThread is very busy and the application is in background

问题描述

I've noticed the following weird behaviour when my GLThread is doing an intensive work.

Launch the application and wait until the GLThread does an intensive task. Send the application to background Send a push notification from firebase Wait until ANR show up (because the application cannot wake up the Notification Service)

If instead of sending the application to background I just wait with the application in foreground, the notification arrives and the ANR does not show up.

In theory GLThread is not the main thread so I should be able to do any kind of work in there without getting an ANR.

You can find my project here: https://issuetracker.google.com/issues/113119768

In order to reproduce my "intensive work" i just put an infinite loop on the onDrawFrame Function

public void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    while(true)
    {
        Log.e("GLThread", "draw");
    }
}

Any help would be very appreciated.

标签: androidopengl-esbackgroundandroid-anr-dialog

解决方案


问题是这个无限循环 onDrawFrame() 回调,通过这样的操作,您将锁定 GL 线程。

我想 GL 线程来自 GLSurfaceView 或类似的?您必须了解,您不能在此方法中放置无限循环。onDrawFrame() 函数只是每次表面准备好绘制新帧时由 GL 线程触发的回调。如果您不允许回调继续,那么您将阻塞 GL 线程。

因此,除了不允许工作流继续之外,与 GL 线程通信的其他线程也将被锁定。例如,当 UI 线程告诉 GLSurface 停止时,在这种情况下会导致 ANR,因为 onDrawFrame() 方法由于无限循环而持有当前的 GL 同步锁。


推荐阅读