如何在打开应用程序时删除黑屏

时间:2017-08-02 13:16:45

标签: java android

我有一个启动画面,只在新的应用程序启动时显示。如果用户点击后退按钮并再次启动应用程序,则不会显示启动。一切都很好,直到这里,如果没有显示飞溅,打开应用程序时有1-2秒黑屏。这是我的splashactivity java文件;

public class SplashScreen extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("first_time", false)) // if first time, show splash
{

    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("first_time", true);
    editor.commit();


    setContentView(R.layout.activity_splash);


    Thread t = new Thread() {
        public void run() {
            try {
                int time = 0;
                while (time < 4000) {
                    sleep(100);
                    time += 100;
                }
            }
            catch (InterruptedException e) {
                // do nothing
            }
            finally {

                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);
                finish();
            }
        }
    };
    t.start();
}
else // if not first time, dont show splash
{
    setContentView(R.layout.activity_splash);
    Intent i = new Intent(SplashScreen.this, MainActivity.class);
    startActivity(i);
    finish();
}

如何解决此问题?

5 个答案:

答案 0 :(得分:1)

在应用程序样式中添加:

<item name="android:windowDisablePreview">true</item>

答案 1 :(得分:1)

因为您在显示启动画面之前检查是否是“第一次”打开应用程序:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("first_time", false)) {
    // Show splash
} else {
    // Don't show splash
}

首先创建启动画面不是right way。您不应该通过创建计时器让您的用户等待启动屏幕。 Instead,只要您的应用加载即可显示启动画面。

为此,您应创建一个简单的图层列表drawable,以用作启动活动的背景:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- A solid background fill -->
    <item
        android:drawable="@color/gray"/>
    <item>

    <!-- A centered logo -->
    <bitmap
        android:gravity="center"
        android:src="@mipmap/ic_launcher"/>
    </item>

</layer-list>

然后将此图层列表用作您在启动活动中使用的主题的background

<resources>

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

    <!-- Splash Screen theme -->
    <style name="SplashTheme">
        <item name="android:background">@drawable/background_splash</item>
        <item name="android:windowAnimationStyle">@null</item>
    </style>

</resources>

将该主题应用于清单中的启动活动:

<activity android:name=".SplashScreen"
    android:theme="@style/SplashTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

然后,最后,您在启动活动中所要做的就是启动MainActivity

public class SplashScreen extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

确保在您的启动活动中调用setContentView(),因为这会为启动画面添加不必要的加载时间。

答案 2 :(得分:1)

 /Just use the below code Snipeet/

Add following code in Style.xml file

<style name="Theme.Transparent" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsTranslucent">true</item>

</style>

 And then use this style in AndroidManifest inside application Tag
 Like this: 
 android:theme="@style/Theme.Transparent"

答案 3 :(得分:1)

您必须将启动画面背景颜色作为应用程序主题颜色。

<activity
        android:name=".SplashScreen"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/SplashScreenTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

在您的values / style.xml中设置样式`

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<style name="SplashScreenTheme" parent="AppBaseTheme">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowBackground">@color/white</item>
    <item name="colorPrimaryDark">@color/white</item>
</style>

在values-v21 / style.xml中设置样式

 <style name="SplashScreenTheme" parent="AppBaseTheme">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowBackground">@color/white</item>
    <item name="android:colorPrimaryDark">@color/white</item>
</style>

答案 4 :(得分:1)

添加@Bryan我建议你cold start。例如,我确实喜欢这个

在你的Style.xml中:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

<style name="AppTheme.splashScreenLauncher">
    <item name="android:windowBackground">@drawable/splash_screen</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>

</style>

splash_screen.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
    <!-- The background color, preferably the same as your normal theme -->
    <item android:drawable="@android:color/white"/>

    <item>
        <bitmap
            android:src="@drawable/SPLASHIMAGE"
            android:gravity="center"/>
    </item>
</layer-list>

在清单中,您设置启动器活动:

android:theme="@style/AppTheme.splashScreenLauncher"> 

在您的启动器活动中:

setTheme(R.style.AppTheme); // IMPORTANT BEFORE super.onCreate
super.onCreate(savedInstanceState);