Android_SplashActivity

功能实现:

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:



    




你可能感兴趣的:(Android_SplashActivity)