android -splash屏幕显示空白

时间:2017-01-24 13:18:06

标签: android splash-screen splash

启动活动只显示空白屏幕。代码如下:

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

public class SplashScreen extends AppCompatActivity {
    private static int SPLASH_TIME_OUT = 3000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);

        try {

            Log.v(" gonna  AsyncTask"," execute AsyncTask");
            new AsyncTask<String, String, String>() {

                @Override
                protected void onPreExecute() {
                    Log.v(" PreExecute AsyncTask"," execute AsyncTask");
                }

                @Override
                protected String doInBackground(String... arg0) {
                    Log.v(" in doInBackground"," execute AsyncTask");
                    try {

                        Log.v(" sleep doInBackground"," execute AsyncTask");
                        Thread.sleep(SPLASH_TIME_OUT);

                    } catch (Exception e) {
                        Log.v(" Exception "," execute AsyncTask");
                        e.printStackTrace();
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(String unused) {

                    Log.v(" onPostExecute "," execute AsyncTask");
                    Intent i = new Intent(SplashScreen.this,
                            MainActivity.class);
                    Log.v(" starting  Activity "," execute AsyncTask");
                    startActivity(i);

                }
            }.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }




    }// end of onCreate

}// end of class SplashScreen

XML布局文件包含:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_splash_screen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.istiaqueahmed.archeology.SplashScreen">

<ImageView

    android:id="@+id/splash_img"
    android:layout_width="170dp"
    android:layout_height="112dp"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:layout_centerInParent="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="130dp"
    android:src="@drawable/splash"
    />

<TextView
    android:id="@+id/developed_by"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:text="@string/developed_by"
    android:layout_below="@id/splash_img"
    android:textStyle="bold"
    android:textSize="13sp"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="30dp"

    />


</RelativeLayout>

Android清单文件有 -

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        >
        <activity
            android:name=".SplashScreen"
            android:noHistory="true"
            android:screenOrientation="portrait"

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

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

         ........
         </application>

style.xml有 -

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

也许活动需要一段时间才能执行setContentView,同时应用转到MainActivity

如何阻止应用显示空白屏幕,而是显示预期时间段的启动画面?

3 个答案:

答案 0 :(得分:0)

你的应用程序正在冻结,因为你正在睡觉主线程。

Thread.sleep(SPLASH_TIME_OUT); // This will sleep your main thread.

而不是AsyncTask延迟使用Handler

答案 1 :(得分:0)

使用CountDownTimer代替AsyncTask

private static long SPLASH_TIME_OUT = 3000;

new CountDownTimer(SPLASH_TIME_OUT, 1000) {

        public void onTick(long millisUntilFinished)
        {

        }

        public void onFinish()
        {
            Intent i = new Intent(SplashScreen.this,
                        MainActivity.class);
             startActivity(i);
        }
    }.start();

答案 2 :(得分:0)

There is no need to used AsyncTask here.
Please try this code which helps you.

public class WelcomeActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);
    startHandler();
}

private void startHandler() {
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            Intent i = new Intent(WelcomeActivity.this, MainActivity.class);
            startActivity(i);
            finish();

        }
    };
    Handler handler = new Handler();
    handler.postDelayed(runnable, 3000);
}

}