Android自定义短信倒计时view流程分析

来自:网络
时间:2020-10-14
阅读:

倒计时实现有三种方式 而这个自定义view是通过handler实现的。为了保证activity销毁的同时倒计时线程依然进行同时重新创建销毁又不会导致内存泄漏,我使用了handler的弱引用将handler和runnable设置成静态,同时通过一系列变量来销毁关闭线程保存状态,话不多说先看效果图:

Android自定义短信倒计时view流程分析

下面看源码:

 import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import androidx.annotation.NonNull;
import com.juexing.payassistant.activity.login.RegisterActivity;
import java.lang.ref.WeakReference;
//待优化为了保存倒计时状态线程延迟一秒关闭所有的子线程
//qq也是如此
public class CountDownTimeView extends androidx.appcompat.widget.AppCompatButton {
  private static volatile int i=60;
  //用来保存倒计时时间的中转变量
  private static volatile int j=0;
  private static TimeHandler handler;
//每次启动活动判断i是否为0来读取倒计时时间
  public static int getI() {
    return i;
  }
  //RegisterActivity是调用该控件的活动需要在oncreate中将对象传入
public void setActivity(RegisterActivity activity){
    handler=new TimeHandler(activity);
}
  public static void setI(int i) {
    CountDownTimeView.i = i;
  }
  //防止多次重复点击发送设置的变量
  private static volatile boolean send=true;
  private class TimeHandler extends Handler {
    WeakReference<RegisterActivity> timeActivityWeakReference;
 public TimeHandler(RegisterActivity registerActivity){
   this.timeActivityWeakReference = new WeakReference<>(registerActivity);
 }
    @Override
    public void handleMessage(@NonNull Message msg) {
      super.handleMessage(msg);
      RegisterActivity activity=timeActivityWeakReference.get();//获取活动
      switch (msg.what){
        case 1:
          if(i>0){
            activity.getmessage. setText(i+"s");
          }else {
          }
          break;
        case 2:
          activity.getmessage. setText("重新获取");
          break;
        case 3:
          i=j-1;
          j=0;
          send=false;
          handler.postDelayed(runnable,1000);
      }
    }
  };
  public CountDownTimeView(Context context) {
    super(context);
  }
  public CountDownTimeView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }
//网络请求短信成功后调用
  public void onStart(CountDownTimeView view){
   if(send){
     if(i==0){
       i=60;
     }
     handler.postDelayed(runnable,1000);
     view.setText(i+"s");
     send=false;
   }else {
       //为了显示不突兀自动减去一秒
       view.setText(i-1+"s");
       j=i;
       //将i设置为0是将所有线程运行完毕关闭释放内存
       i=0;
       handler.removeCallbacksAndMessages(null);
       //因为线程调度随机性所以要延迟一秒发送保证子线程关闭
       Message message=Message.obtain();
       message.what=3;
       handler.sendMessageDelayed(message,1000);
//     i=j;
   }
  }
 //设置成静态保证唯一性
  public static Runnable runnable=new Runnable() {
    @Override
    public void run() {
      if(i>0){
        i--;
        Message message=new Message();
        message.what=1;
        handler.handleMessage(message);
        handler.postDelayed(runnable,1000);

      }else {
        if(j!=0){

        }else {
          send=true;
          Message message=new Message();
          message.what=2;
          handler.handleMessage(message);
        }

      }
    }
  };
}

在activity的oncreate中需要将activity实例对象传入,并进行判断:

 getmessage.setActivity(this);
    if(getmessage.getI()<60){
      if(getmessage.getI()>0){
        getmessage.onStart(getmessage);
      }else {
        getmessage.setText("重新获取");
      }

    }

  }

```getmessage是自定义view的实例对象。

如要源码可以到我的git上获取:

[https://github.com/heybixby/CountDownTimerView```](https://github.com/heybixby/CountDownTimerView)

返回顶部
顶部