启动屏幕不会先打开

时间:2017-03-10 13:00:06

标签: android

不是先打开启动画面,而是打开主要活动,然后是启动画面。可能是什么问题。启动画面不会消失,直到我按下按钮。

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.os.Handler;

public class MainActivity extends AppCompatActivity {
  private static int SPLASH_TIME_OUT=4000;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  new Handler().postDelayed(new Runnable(){
     public void run(){
     Intent homeIntent=new Intent(MainActivity.this,Splash.class);
     startActivity(homeIntent);
     finish();
     }
        }, SPLASH_TIME_OUT);
   }
}

2 个答案:

答案 0 :(得分:2)

制作Splashscreen的最佳方式

你需要什么:

  • 两个ActivityMainActivity.javaSplashScreen.java
  • 两种布局:layout_main_activity.xmllayout_splashscreen.xml

在AndroidManifest.xml中,声明两个Activity。 SplashScreen必须是主体ActivityMAINLAUNCHER):

        <!-- Splash screen -->
        <activity
            android:name="com.package.SplashScreen"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <!-- Main activity -->
        <activity
            android:name="com.package.MainActivity"
            android:label="@string/app_name" >
        </activity>

SplashScreen.java:

public class SplashScreen extends Activity {

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

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);
                finish();
            }
        }, 3000); // for 3 seconds
    }

}

答案 1 :(得分:0)

在AndroidManifest.xml中,

<activity
    android:name=".SplashActivity"
    android:noHistory="true"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".MainActivity"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen" />

在splashscreen.java onCreate函数中,您可以设置一个计时器来调用mainactivity

new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                startActivity(new Intent(getApplicationContext(), MainActivity.class));
            }
        },4000);

希望这有帮助