简体   繁体   中英

Android: Splash screen not showing

So I am trying to get a splash screen to work, but I can't figure something out. I get a white screen during the splash screen even though it should be a different color and contain text. Here is my manifest file:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.haas.ryan.bouncingball">

    <!-- Gives permission to create directories and files -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_E

    <application
        android:allowBackup="true"
        android:icon="@mipmap/bb_logo"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".splash.SplashActivity"
            android:screenOrientation="userLandscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".bbsource.MainActivity"
            android:screenOrientation="userLandscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

Here are my 2 Splash screen related classes:

SplashActivity.java

package com.haas.ryan.bouncingball.splash;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import com.haas.ryan.bouncingball.bbsource.MainActivity;

public class SplashActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(new SplashEnvironment(this, this));
    }

    public void finish() {
        startActivity(new Intent(this, MainActivity.class));
        super.finish();
    }
}

SplashEnvironment.java

package com.haas.ryan.bouncingball.splash;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by Ryan on 7/31/2017.
 */

public class SplashEnvironment extends SurfaceView implements SurfaceHolder.Callback {
    private SplashActivity splashActivity;

    public SplashEnvironment(Context context, SplashActivity splashActivity) {
        super(context);
        this.splashActivity = splashActivity;
        getHolder().addCallback(this);
    }

    public void surfaceCreated(SurfaceHolder sh) {
        try {
            Thread.sleep(10000);
        } catch(Exception e) {
            e.printStackTrace();
        }
        splashActivity.finish();
    }
    public void surfaceDestroyed(SurfaceHolder sh) {}
    public void surfaceChanged(SurfaceHolder sh, int format, int width, int height) {}

    public void onDraw(Canvas canvas) {
        draw(canvas);
    }

    public void draw(Canvas canvas) {
        super.draw(canvas);
        canvas.drawColor(Color.RED);
        System.out.println("got here");
        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setTextSize(40f * getResources().getDisplayMetrics().density);
        canvas.drawText("SPLASH SCREEN", 100, 100, paint);
    }
}

The white screen goes away after the duration and eventually loads bbsource.MainActivity and draws everything that it is supposed to.

So why am I getting a white screen when I clearly tell it to draw other things?

try this

public class SplashActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(new SplashEnvironment(this, this));
    new Handler().postDelayed(new Runnable() {

    /*
     * Showing splash screen with a timer. This will be useful when you
     * want to show case your app logo / company
     */

    @Override
    public void run() {
        startActivity(new Intent(SplashActivity.this, MainActivity.class));
        finish();
        // close this activity
    }
}, 3000);// time for spalsh screen
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM