android 锁屏崩溃,Libgdx解决部分Android机型锁屏崩溃的方法

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就继承这个自定义的类就行。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值