package com.yuyidong.second;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;
public class Splash extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
new Handler().postDelayed(new Runnable(){
// 为了减少代码使用匿名Handler创建一个延时的调用
public void run() {
Intent i = new Intent(Splash.this, MainActivity.class);
//通过Intent打开最终真正的主界面Main这个Activity
Splash.this.startActivity(i); //启动Main界面
Splash.this.finish(); //关闭自己这个开场屏
}
}, 5000); //5秒
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/ic_launcher"
android:layout_gravity="center"
android:contentDescription="Hello"
/>
<TextView
android:id="@+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="This is a splash!!"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yuyidong.second"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.yuyidong.second.MainActivity"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
使用Activity的唯一缺点就是它无法利用Splash显示的时间里做数据加载。因为它是一个单独的Activity,无法控制其他的Activity,并且这时其他Activity还未创建。