你需要:
- 添加一个新的 Activity
SplashActivity
,它将成为您应用的启动 Activity。
- 当您的应用程序启动时,启动您的应用程序
SplashActivity
并等待一段时间。
- 等待完成后,开始
MainActivity
并完成SplashActivity
.
这是一个很好的例子:
闪屏活动:
public class SplashScreenActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout); //You need to define it in your layouts
final int welcomeScreenDisplay = 3000; //splash lasts for 3 sec. You can change it
/** create a thread to show splash up to splash time */
Thread welcomeThread = new Thread() {
int wait = 0;
@Override
public void run() {
try {
super.run();
while (wait < welcomeScreenDisplay) {
sleep(100);
wait += 100;
}
} catch (Exception e) {
System.out.println("EXc=" + e);
} finally {
startActivity(new Intent(SplashScreenActivity.this, MainScreenActivity.class));
finish();
}}
};
welcomeThread.start();
}
}
并且不要忘记将活动添加到您的清单文件中:
<activity
android:name=".SplashScreenActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainScreenActivity"
android:label="@string/app_name" >
</activity>