使用的是 Android studio 3.0.1
生成工程,新建启动页文件
package com.example.administrator.my_flashpage;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class SplashActivity extends AppCompatActivity {
private Handler handler = new Handler();
private static final String TAG = "SplashActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//不显示标题和状态栏,相当于全屏
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);
//延时
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
}
}, 2000);
}
}
为了区分和主界面的区分修改 界面文件
内容为
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:gravity="center"
android:textColor="#FF0000"
android:layout_gravity="center"
android:text="app启动画面,Hello dragon" />
</LinearLayout>
返回主界面 MainActivity
package com.example.administrator.my_flashpage;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//下面检测返回键如果有按反回就直接退出主界面
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode== KeyEvent.KEYCODE_BACK){
Intent i= new Intent(Intent.ACTION_MAIN); //主启动,不期望接收数据
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //新的activity栈中开启,或者已经存在就调到栈前
i.addCategory(Intent.CATEGORY_HOME); //添加种类,为设备首次启动显示的页面
startActivity(i);
}
return super.onKeyDown(keyCode, event);
}
}
最后修改 AndroidManifest.xml ,让运行先去执行 启动的界面
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.my_flashpage">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!--这里是启动界面 -->
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--这里是主界面 -->
<activity android:name=".MainActivity">
</activity>
</application>
</manifest>