Libgdx解決部分Android機型鎖屏崩潰的方法
libgdx使用了全屏模式之后,在某些機型會出現(xiàn)崩潰的情況,兩年前就存在了,一直到現(xiàn)在為止,官方都沒進行修復,其崩潰原因就是在源碼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毫秒,執(zhí)行一個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就繼承這個自定義的類就行。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
- 詳解 Android中Libgdx使用ShapeRenderer自定義Actor解決無法接收到Touch事件的問題
- 詳解Android Libgdx中ScrollPane和Actor事件沖突問題的解決辦法
- Android使用libgdx實現(xiàn)模擬方向鍵控制角色移動的方法
- Android 游戲引擎libgdx 資源加載進度百分比顯示案例分析
- Android drawable微技巧,你不知道的drawable細節(jié)
- Android指紋識別API講解,一種更快更好的用戶體驗
- Android在Kotlin中更好地使用LitePal
- Android Studio輕松構建自定義模板的步驟記錄
- 詳解Android 檢測權限的三種寫法
- Android最簡單的狀態(tài)切換布局實現(xiàn)教程
- android自定義環(huán)形對比圖效果
相關文章
Android Studio中導入JNI生成的.so庫的實現(xiàn)方法
這篇文章主要介紹了Android Studio中導入JNI生成的.so庫的實現(xiàn)方法的相關資料,這里不僅提供實現(xiàn)方案并提供了實現(xiàn)的方法,需要的朋友可以參考下2017-07-07
Android開發(fā)之自定義view實現(xiàn)通訊錄列表A~Z字母提示效果【附demo源碼下載】
這篇文章主要介紹了Android開發(fā)之自定義view實現(xiàn)通訊錄列表A~Z字母提示效果,結合完整實例形式分析了Android獲取通訊錄列表及采用自定義view排列顯示的相關操作技巧,需要的朋友可以參考下2017-07-07
深入android Unable to resolve target ''android-XX''詳解
本篇文章是對android Unable to resolve target 'android-XX'錯誤的解決方法進行了詳細的分析介紹,需要的朋友參考下2013-06-06

