Android: The splash screen does not get shown, why

2019-06-23 16:20发布

问题:

I am working on learning Android. From the documents I have read so far I can't figure out how to get the splash View to show (during the sleep the screen stays blank). It appears I need to start a new activity for the main layout, but this seems wasteful (the splash should be forever gone, I'd like to reuse its thread).

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class Ext3 extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        Log.v("Ext3", "starting to sleep");

        try {
            Thread.sleep (5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Log.v("Ext3", "done sleeping");

        setContentView (R.layout.main);
    }

}

回答1:

I believe your splash screen never gets shown because you never give the UI thread (that you are in) a chance to draw it since you just sleep there doing nothing.

Instead of the Thread.sleep, I would suggest you look into a Timer or something like that to schedule the refresh and changing the content of your view; an alternative would be to start an AsyncTask where you could sleep before changing the view as you are doing now.

Do not sleep or in any other way block the UI thread, that's bad... (causes ANR)



回答2:

When you call sleep like that, you are blocking the UI thread. Instead, put the second call to setContentView in a Runnable, create a Handler, and use the Handler's postDelayed method to run the Runnable. Something like this:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    Runnable endSplash = new Runnable() {
        @Override
        public void run() {
            setContentView (R.layout.main);
        }
    }
    new Handler().postDelayed(endSplash, 5000L);
}


回答3:

I have tried this code in my application and its working perfectly.May be it will help you.

public class SplashScreen extends Activity {

    /**
     * The thread to process splash screen events
     */
    private Thread mSplashThread;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Splash screen view
        setContentView(R.layout.splash);



        final SplashScreen sPlashScreen = this;

        // The thread to wait for splash screen events
        mSplashThread = new Thread() {
            @Override
            public void run() {
                try {
                    synchronized (this) {
                        // Wait given period of time or exit on touch
                        wait(5000);
                    }
                } catch (InterruptedException ex) {
                }

                finish();

                // Run next activity
                Intent intent = new Intent();
                intent.setClass(sPlashScreen, MainActivity.class);
                startActivity(intent);
                stop();
            }
        };

        mSplashThread.start();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        return false;
    }

    /**
     * Processes splash screen touch events
     */
    @Override
    public boolean onTouchEvent(MotionEvent evt) {
        if (evt.getAction() == MotionEvent.ACTION_DOWN) {
            synchronized (mSplashThread) {
                mSplashThread.notifyAll();
            }
        }
        return true;
    }

}


回答4:

This is the snippet for a basic splash screen

public class Splash extends Activity {

//private ProgressDialog pd = null;
private final int SPLASH_DISPLAY_LENGTH = 3000; 

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.splashscreen);
    //this.pd = ProgressDialog.show(this, "Initializing..", "Initializing Infraline...", true, false);

    /* New Handler to start the InfralineTabWidget-Activity
     * and close this Splash-Screen after some seconds.*/

    new Handler().postDelayed(new Runnable(){
        @Override
        public void run() {
        /* Create an Intent that will start the InfralineTabWidget-Activity. */
            Intent mainIntent = new Intent(Splash.this,InfralineTabWidget.class);
            Splash.this.startActivity(mainIntent);
            Splash.this.finish();
        }
    }, SPLASH_DISPLAY_LENGTH);

}

}

And in your AndroidManifest.xml put

    <activity android:name=".Splash" android:theme="@android:style/Theme.NoTitleBar" android:configChanges="orientation|keyboardHidden">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Hopefully this works for you :)