Libgdx解决部分Android机型锁屏崩溃的方法
libgdx使用了全屏模式之后,在某些机型会出现崩溃的情况,两年前就存在了,一直到现在为止,官方都没进行修复,其崩溃原因就是在源码AndroidGraphics.java中的onPause可以看到这样子的一段代码:
voidpause(){
synchronized(synch){
if(!running)return;
running=false;
pause=true;
while(pause){
try{
//TODO:fixdeadlockraceconditionwithquickresume/pause.
//Temporaryworkaround:
//AndroidANRtimeis5seconds,sowaitupto4secondsbeforeassuming
//deadlockandkillingprocess.Thiscaneasilybetriggeredbyopeningthe
//RecentAppslistandthendouble-tappingtheRecentAppsbuttonwith
//~500msbetweentaps.
synch.wait(4000);
if(pause){
//pausewillnevergofalseifonDrawFrameisnevercalledbytheGLThread
//whenenteringthismethod,weMUSTenforcecontinuousrendering
Gdx.app.error(LOG_TAG,"waitingforpausesynchronizationtooktoolong;assumingdeadlockandkilling");
android.os.Process.killProcess(android.os.Process.myPid());
}
}catch(InterruptedExceptionignored){
Gdx.app.log(LOG_TAG,"waitingforpausesynchronizationfailed!");
}
}
}
}
崩溃的提示就是在这个方法中进行抛出的,解决方法就是,不让他抛出这个错误,就是在try里面把pause改为false,目前的解决方法是这样子,静候官方的修复了,自定义一个类,例如我用的是AndroidFragmentApplication,我自定义一个PatchedAndroidFragmentApplication,在onPause之后利用线程延迟100毫秒,执行一个onDrawFrame,使得pause为false即可:
openclassPatchedAndroidFragmentApplication:AndroidFragmentApplication(){
privatevalexec=Executors.newSingleThreadExecutor()
privatevalforcePause=Runnable{
try{
Thread.sleep(100)
}catch(e:InterruptedException){
}
graphics.onDrawFrame(null)
}
overridefunonPause(){
if(activity!!.window.attributes.flagsandWindowManager.LayoutParams.FLAG_FULLSCREEN==WindowManager.LayoutParams.FLAG_FULLSCREEN){
//是全屏
exec.submit(forcePause)
}
super.onPause()
}
}
然后你的Fragment就继承这个自定义的类就行。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接