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:

<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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值