前言
Android啟動(dòng)出現(xiàn)黑屏(或白屏) ,按照出現(xiàn)的時(shí)間不同,可以分為兩種情況。
- 啟動(dòng)到閃屏頁面(splash screnn)出現(xiàn)前,出現(xiàn)黑屏。
- 閃屏頁面消失到APP首頁出現(xiàn)之前,出現(xiàn)黑屏。
閃屏頁面出現(xiàn)前
在這種情況下出現(xiàn)黑屏,要想做優(yōu)化,只能通過修改原生代碼來實(shí)現(xiàn)。
第一步,修改app/res/values/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme.Holo.Light"></style>
<style name="Theme.AppStartLoadTranslucent" parent="android:Theme">
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowDisablePreview">true</item>
</style>
</resources>
第二步,修改app/manifests/AndroidManifest.xml
修改MainActivity的theme為 "@style/Theme.AppStartLoadTranslucent"
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTask" android:name="MainActivity" android:screenOrientation="portrait" android:theme="@style/Theme.AppStartLoadTranslucent" android:windowSoftInputMode="adjustResize">
閃屏頁面出現(xiàn)后
在這種情況下出現(xiàn)黑屏,相對(duì)比較容易優(yōu)化。
把閃屏頁面消失修改為不自動(dòng)消失,通過代碼來延遲閃屏頁面的消失。
第一步,修改config.xml中的AutoHideSplashScreen為false
<preference name="AutoHideSplashScreen" value="false" />
第二步,隱藏閃屏頁面
在app.js中通過代碼延時(shí)隱藏閃屏頁面,避免出現(xiàn)黑屏
// 延遲splash screnn 隱藏時(shí)間,不然會(huì)有短暫的白屏出現(xiàn)
if (navigator.splashscreen) {
setTimeout(function () {
navigator.splashscreen.hide();
}, 300);
}