Libgdx解决部分Android机型锁屏崩溃的方法
程序员文章站
2023-12-09 21:59:57
libgdx使用了全屏模式之后,在某些机型会出现崩溃的情况,两年前就存在了,一直到现在为止,官方都没进行修复,其崩溃原因就是在源码androidgraphics.java中...
libgdx使用了全屏模式之后,在某些机型会出现崩溃的情况,两年前就存在了,一直到现在为止,官方都没进行修复,其崩溃原因就是在源码androidgraphics.java中的onpause可以看到这样子的一段代码:
void pause () { synchronized (synch) { if (!running) return; running = false; pause = true; while (pause) { try { // todo: fix deadlock race condition with quick resume/pause. // temporary workaround: // android anr time is 5 seconds, so wait up to 4 seconds before assuming // deadlock and killing process. this can easily be triggered by opening the // recent apps list and then double-tapping the recent apps button with // ~500ms between taps. synch.wait(4000); if (pause) { // pause will never go false if ondrawframe is never called by the glthread // when entering this method, we must enforce continuous rendering gdx.app.error(log_tag, "waiting for pause synchronization took too long; assuming deadlock and killing"); android.os.process.killprocess(android.os.process.mypid()); } } catch (interruptedexception ignored) { gdx.app.log(log_tag, "waiting for pause synchronization failed!"); } } } }
崩溃的提示就是在这个方法中进行抛出的,解决方法就是,不让他抛出这个错误,就是在try里面把pause改为false,目前的解决方法是这样子,静候官方的修复了,自定义一个类,例如我用的是androidfragmentapplication,我自定义一个patchedandroidfragmentapplication,在onpause之后利用线程延迟100毫秒,执行一个ondrawframe,使得pause为false即可:
open class patchedandroidfragmentapplication : androidfragmentapplication() { private val exec = executors.newsinglethreadexecutor() private val forcepause = runnable { try { thread.sleep(100) } catch (e: interruptedexception) { } graphics.ondrawframe(null) } override fun onpause() { if (activity!!.window.attributes.flags and windowmanager.layoutparams.flag_fullscreen == windowmanager.layoutparams.flag_fullscreen) { // 是全屏 exec.submit(forcepause) } super.onpause() } }
然后你的fragment就继承这个自定义的类就行。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接