API 21之前的Android 12闪屏

Ram*_*him 16 android splash-screen android-12

我正在尝试将我的应用程序升级到目标 android 31,它引入了闪屏 API,因此我遵循了此处提到的迁移过程,但迁移后应用程序无法运行,因为闪屏 API 仅支持 android 版本 21 及以上版本,所以什么是支持 21 之前版本的流程?

在此输入图像描述

Ram*_*him 9

Androidx SplashScreen类的工作原理如下:

在 API 31+ (Android 12+) 上,此类调用平台方法。

在之前的 API 31 中,平台行为被复制,但启动屏幕上的动画矢量可绘制对象支持除外。

问题是Animated Vector Drawable类是在 Android 21 中引入的,因此当前的 Androidx SplashScreen 类向后兼容 Android +21,所以我找到了一些解决方案。我创建了两个不同的启动屏幕活动,一个用于处理旧版本,另一个使用 Androidx SplashScreen API。我根据当前的android版本制作了系统午餐启动画面,所以我做了以下操作

1-为了允许应用程序编译和构建,我必须将此行添加到我的清单文件中

<uses-sdk tools:overrideLibrary="androidx.core.splashscreen"/>
Run Code Online (Sandbox Code Playgroud)

2- 按照文档中的迁移步骤创建使用 Androidx SplashScreen API 的新启动屏幕活动。

3-在values文件夹下创建bools.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="new_splash_enabled">false</bool>
    <bool name="old_splash_enabled">false</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)

4-在values-vX(X是minSdkVersion)中覆盖bools.xml,在我的例子中是16,所以我在同一级别的values文件夹中创建了values-v16文件夹,并在其下创建bools.xml,如下所示

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="new_splash_enabled">false</bool>
    <bool name="old_splash_enabled">true</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)

5-覆盖values-vX中的bools.xml(X是您想要应用新SplashScreen的最低版本,因此它是21到31之间的任何数字)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="new_splash_enabled">true</bool>
    <bool name="old_splash_enabled">false</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)

6-在您的清单中,我让系统根据 bools.xml 文件中的值决定启动哪个启动活动

        <activity
            android:name=".NewSplash"
            android:theme="@style/Theme.App.Starting"
            android:enabled="@bool/new_splash_enabled"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".OldSplash"
            android:enabled="@bool/old_splash_enabled">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
Run Code Online (Sandbox Code Playgroud)


J R*_*Rom 7

如果您使用的是 Androidx 兼容库,请确保您使用的属性不带 android 标记。例如:

这会工作得很好

<item name="windowSplashScreenBackground">?colorPrimary</item>
Run Code Online (Sandbox Code Playgroud)

这会给你一个“需要 API 31”错误

<item name="android:windowSplashScreenBackground">?colorPrimary</item>
Run Code Online (Sandbox Code Playgroud)