闪屏在android上的react-native应用中不起作用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了闪屏在android上的react-native应用中不起作用相关的知识,希望对你有一定的参考价值。

我有react-native应用,我需要在android上实现启动屏幕。我的步骤是:将png图像放入app / src / main / res / mibmap,在app / src / main / res / drawable /中创建background_splash.xml,将SplashTheme添加到app / src / main / res / values / styles.xml ,更改AndroidManifest.xml,更改MainActivity.java,在src / main / java /中创建SplashActivity.java类。但是我的启动画面没有出现在应用程序中。您能告诉我我在做什么错吗?

background_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/black" />

    <item>
        <bitmap
            android:src="mipmap/splash_screen"
            android:gravity="center" />
    </item>
</layer-list>

styles.xml

<resources xmlns:tools="http://schemas.android.com/tools">

    <color name="primary_dark">#27409d</color>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->

        <!--<item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="android:statusBarColor" tools:targetApi="lollipop">@android:color/transparent</item>-->

<!--        fix https://trello.com/c/OuBMJCMI-->
<!--        https://github.com/facebook/react-native/issues/17530-->
        <item name="android:editTextBackground">@android:color/transparent</item>
    </style>

    <style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@drawable/background_splash</item>
    </style>


</resources>

colors.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>
    <color name="black">#000000</color>
</resources>

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="broker.mapp"
          android:fitsSystemWindows="false">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.USE_FINGERPRINT"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

    <uses-sdk />

    <application
            android:name="broker.mapp.MainApplication"
            android:allowBackup="false"
            android:label="@string/app_name"
            android:icon="@mipmap/ic_launcher"
            android:theme="@style/AppTheme"
            android:networkSecurityConfig="@xml/network_security_config"
            android:largeHeap="true"
    >


        <activity
            android:name="broker.mapp.SplashActivity"
            android:theme="@style/SplashTheme"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <activity
                android:name="broker.mapp.MainActivity"
                android:label="@string/app_name"
                android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
                android:windowSoftInputMode="adjustResize"
                android:screenOrientation="portrait"
                android:launchMode="singleTask"
                android:exported="true">
            <intent-filter android:label="filter_react_native">
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="broker" android:host="*"/>
            </intent-filter>
        </activity>


        <activity android:name="com.facebook.react.devsupport.DevSettingsActivity"/>
        <meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_name"
                    android:value="Без категории"/>

                    android:value=" "/>

            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

        <meta-data android:name="com.google.firebase.messaging.default_notification_icon"

    </application>

</manifest>

MainActivity.java

package broker.mapp;

import android.os.Bundle;


import com.facebook.react.ReactActivity;
import io.fabric.sdk.android.Fabric;
import com.crashlytics.android.core.CrashlyticsCore;
import com.crashlytics.android.Crashlytics;
import android.content.Intent;
import android.content.res.Configuration;
import org.devio.rn.splashscreen.SplashScreen;

public class MainActivity extends ReactActivity 

    /**
     * Returns the name of the main component registered from javascript.
     * This is used to schedule rendering of the component.
     */


    @Override
    protected String getMainComponentName() 
        return "BrokerMapp";
    





    @Override
    protected void onCreate(Bundle savedInstanceState) 
        SplashScreen.show(this, true);
        super.onCreate(savedInstanceState);

        CrashlyticsCore core = new 
        CrashlyticsCore.Builder().disabled(BuildConfig.USE_FABRIC != 
        "TRUE").build();
        Fabric.with(this, new 
        Crashlytics.Builder().core(core).build());
    


    public void onConfigurationChanged(Configuration newConfig) 
        super.onConfigurationChanged(newConfig);
        Intent intent = new Intent("onConfigurationChanged");
        intent.putExtra("newConfig", newConfig);
        this.sendBroadcast(intent);
    

SplashActivity.java

package com.app;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class SplashActivity extends AppCompatActivity 
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, rshb.broker.mapp.MainActivity.class);
        startActivity(intent);
        finish();
    

答案

尝试下面的onCreate方法中的代码

  new Handler().postDelayed(new Runnable() 
                      @Override

                  public void run() 

                         Intent intent = new Intent(SplashActivity.this,MainActivity.class);
                         startActivity(intent);
                         finish();
                  
              , 3000);

以上是关于闪屏在android上的react-native应用中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

如何禁用 Android 上的设备后退按钮(react-native)?

带有 React-Native 的 android 模拟器上的位置请求超时

使用 react-native 的 android 上的空白白屏(仅发布)

Android 上的 react-native 运行教程“AwesomeProject”导致“无法下载 JS Bundle”红屏

React-Native SectionList 滚动到Android上的项目

修复 React-Native run-android 上的构建失败