Android_SplashActivity
来源:互联网 发布:魔镜大数据可视化 编辑:程序博客网 时间:2024/06/03 11:33
功能实现:
1、倒计时变成0时,自动跳转:Handler、Timer、CountDownTimer
2、SharedPreferences记录是否首次启动app:首次跳转引导页,否则跳转主页
SplashActivity:
public class SplashActivity extends AppCompatActivity { public static final int MSG_UPDATE_TIME = 1; // 时间间隔1s public static final int INTERNAL_TIME = 1000; // 配置文件名 public static final String CONGIF_NAME = "app_config"; // 配置文件中的一个配置项:是否首次启动 public static final String IS_FIRST_LOGIN = "is_first_login"; // 倒计时,初始5s private int mCountDownTime = 5; protected TextView mCountDownTv; private Handler mHandler = new Handler(new Handler.Callback() { // 接收发送的消息 @Override public boolean handleMessage(Message msg) { switch (msg.what) { case MSG_UPDATE_TIME:// 更新时间 if (mCountDownTime > 0) { mCountDownTv.setText(--mCountDownTime + "s"); // 每次更新完UI界面,都会在1s后发送消息,通知继续下一次的更新 mHandler.sendEmptyMessageDelayed(MSG_UPDATE_TIME, INTERNAL_TIME); } else {// Toast.makeText(SplashActivity.this, "OK", Toast.LENGTH_SHORT).show(); startOtherActivity(); } break; } return true; } }); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_splash); initView(); // 使用handler每隔1s发送一次消息,通知textView刷新UI界面 mHandler.sendEmptyMessageDelayed(MSG_UPDATE_TIME, INTERNAL_TIME); } private void initView() { mCountDownTv = (TextView) findViewById(R.id.count_down_tv); } // 跳转界面 private void startOtherActivity() { SharedPreferences sharedPreferences = this.getSharedPreferences(CONGIF_NAME, Context.MODE_PRIVATE); // 每次启动app都要检查是否首次启动 boolean isFirstLogin = sharedPreferences.getBoolean(IS_FIRST_LOGIN, true); // 跳转 Intent intent = new Intent(); if (isFirstLogin) {// 跳转引导页 intent.setClass(this, GuideActivity.class); // 从此不再是首次启动 SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean(IS_FIRST_LOGIN, false); editor.commit(); } else {// 跳转主界面 intent.setClass(this, MainActivity.class); } startActivity(intent); // 关闭当前页面 finish(); }}
avtivity_Splash:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="net.bwie.splashactivitydemo.activity.SplashActivity"> <TextView android:id="@+id/count_down_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="5s"/></RelativeLayout>
阅读全文