安卓应用启动页的实现
首先新建一个名为SplashActivity.java的类,这个类会在显示完首页后自动跳转进入主页面
package com.example.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashActivity extends Activity {
Handler mHandler=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
System.out.println(1111111111);
startActivity(intent);
}
}, 3000); //2秒
if (!this.isTaskRoot()) { // 判断当前activity是不是所在任务栈的根
Intent intent = getIntent();
if (intent != null) {
String action = intent.getAction();
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(action)) {
finish();
return;
}
}
}
}
}
第二步,配置xml文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/purple_200" > <ImageView android:id="@+id/splash_bg_img" android:src="@mipmap/start" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" /> </RelativeLayout>
第三步,在AndroidManifest.xml中将SplashActivity设置为首个启动页面
<activity android:name=".SplashActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity>
操作完成