android SplashActivity的两种写法

记录SplashActivity的简单用法,重要在思路

倒计时式(在UI显示倒计时)

使用静态处理和弱引用的方式防止内存泄漏

处理器的SendMessage函数发送第一次消息(索引)

之后再的handleMessage中接受并处理消息(再次发送,一个循环进行的)和UI更新

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.lang.ref.WeakReference;

public class SplashActivity extends AppCompatActivity {

    public static final int CODE = 1001;
    public static final int TOTAL_TIME = 3000;
    public static final int INTERVAL_TIME = 1000;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spash);

        mTextView = (TextView) findViewById(R.id.time_text_view);


        final MyHandler handler = new MyHandler(this);
        //第一次发消息
        Message message = Message.obtain();
        message.what = CODE;
        message.arg1 = TOTAL_TIME;
        handler.sendMessage(message);

        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BookListActivity.start(SplashActivity.this);
                SplashActivity.this.finish();
                //如果点击跳过之后,要去掉这个message防止
                //activity关闭后handler没有被释放掉
                handler.removeMessages(CODE);
            }
        });

    }

    /**
     静态Handler防止内存泄漏
    **/
    public static class MyHandler extends Handler {
        //弱引用
        public final WeakReference mWeakReference;

        public MyHandler(SplashActivity activity) {
            mWeakReference = new WeakReference<>(activity);
        }

        //不断的接收和处理消息
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            SplashActivity activity = mWeakReference.get();
            if (msg.what == CODE) {


                if (activity != null) {

                    // 设置textview,更新UI
                    int time = msg.arg1;
                    activity.mTextView.setText(time/ INTERVAL_TIME +"秒,点击跳过");

                    // 发送倒计时

                    Message message = Message.obtain();
                    message.what = CODE;
                    message.arg1 = time - INTERVAL_TIME;

                    if (time > 0) {
                        sendMessageDelayed(message, INTERVAL_TIME);
                    } else {
                        BookListActivity.start(activity);
                        activity.finish();
                    }

                }
            }
        }
    }

}

定时式,内部实现,不再UI显示倒计时

处理器的postdelayed方法

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;


import com.demo.restaurant.R;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

/**
 * 闪屏页
 * 1.等待3s进入login
 * 2.或者点击跳过进入login
 * 使用了AA注解
 */
@EActivity(R.layout.activity_splash)
public class SplashActivity extends AppCompatActivity {

    @ViewById(R.id.id_splash_btn)
    Button mBtnSkip;

    private Handler mHandler = new Handler();
    private Runnable mRunnableToLogin = new Runnable() {
        @Override
        public void run() {
            toLoginActivity();
        }
    };

    /**
     * 定时事件
     */
    @AfterViews
    void toLoginEvent() {
        //3s后执行runnable
        mHandler.postDelayed(mRunnableToLogin, 3000);
    }

    /**
     * 点击事件
     */
    @Click(R.id.id_splash_btn)
    void mBtnSkip() {
        //如果点击跳过,要去掉定时任务,否则会出现两次splash页面
        mHandler.removeCallbacks(mRunnableToLogin);
        toLoginActivity();
    }

    /**
     * 跳转到Login界面的方法
     */
    private void toLoginActivity() {
        startActivity(new Intent(this, LoginActivity_.class));
        finish();
    }

    /**
     * 防止handle内存泄漏
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacks(mRunnableToLogin);
    }
}

可以两者综合

你可能感兴趣的:(android SplashActivity的两种写法)