Android--利用Handler消息转发机制实现倒计时(内含防止内存泄露处理)

时间:2022-08-06 10:17:54

涉及到的知识点:

1.Handler常用API

2.WeakReference:主线程上下文对象的弱引用


1.Handler常用API

Android--利用Handler消息转发机制实现倒计时(内含防止内存泄露处理)


代码实现:

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

import java.lang.ref.WeakReference;

public class MainActivity extends AppCompatActivity {

/**
* 倒计时标记handle code
*/

public static final int COUNTDOWN_TIME_CODE = 100001;
/**
* 倒计时间隔
*/

public static final int DELAY_MILLIS = 1000;
/**
* 倒计时最大值
*/

public static final int MAX_COUNT = 10;


private TextView mCountdownTimeTextView;


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

//得到控件
mCountdownTimeTextView= (TextView)findViewById(R.id.countdownTimeTextView);

//创建了一个handler
CountdownTimeHandler handler = new CountdownTimeHandler(this);

//新建了一个message
Message message = Message.obtain();
message.what = COUNTDOWN_TIME_CODE;
message.arg1 = MAX_COUNT;

//第一次发送这个message
handler.sendMessageDelayed(message,DELAY_MILLIS);

}

/*防止内存泄露,弱引用的方式获取上下文
* 多用于耗时线程中的操作*/

public static class CountdownTimeHandler extends Handler{
static final int MIN_COUNT = 0;
final WeakReference<MainActivity> mWeakReference;

CountdownTimeHandler(MainActivity activity){
mWeakReference = new WeakReference (activity );
}

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//获取MainActivity
MainActivity activity =mWeakReference.get();

switch (msg.what){
case COUNTDOWN_TIME_CODE:
int value = msg.arg1;
activity.mCountdownTimeTextView.setText(String.valueOf(value --));

//循环发的消息控制
if (value >= MIN_COUNT) {
/*这时需要新建Message对象*/
Message message = Message.obtain();
message.what = COUNTDOWN_TIME_CODE;
message.arg1 = value;
sendMessageDelayed(message, DELAY_MILLIS);
}

break;
}
}
}
}