Android自定義短信倒計(jì)時(shí)view流程分析
倒計(jì)時(shí)實(shí)現(xiàn)有三種方式 而這個(gè)自定義view是通過(guò)handler實(shí)現(xiàn)的。為了保證activity銷毀的同時(shí)倒計(jì)時(shí)線程依然進(jìn)行同時(shí)重新創(chuàng)建銷毀又不會(huì)導(dǎo)致內(nèi)存泄漏,我使用了handler的弱引用將handler和runnable設(shè)置成靜態(tài),同時(shí)通過(guò)一系列變量來(lái)銷毀關(guān)閉線程保存狀態(tài),話不多說(shuō)先看效果圖:
下面看源碼:
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;//待優(yōu)化為了保存倒計(jì)時(shí)狀態(tài)線程延遲一秒關(guān)閉所有的子線程//qq也是如此public class CountDownTimeView extends androidx.appcompat.widget.AppCompatButton { private static volatile int i=60; //用來(lái)保存倒計(jì)時(shí)時(shí)間的中轉(zhuǎn)變量 private static volatile int j=0; private static TimeHandler handler;//每次啟動(dòng)活動(dòng)判斷i是否為0來(lái)讀取倒計(jì)時(shí)時(shí)間 public static int getI() { return i; } //RegisterActivity是調(diào)用該控件的活動(dòng)需要在oncreate中將對(duì)象傳入public void setActivity(RegisterActivity activity){ handler=new TimeHandler(activity);} public static void setI(int i) { CountDownTimeView.i = i; } //防止多次重復(fù)點(diǎn)擊發(fā)送設(shè)置的變量 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();//獲取活動(dòng) 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); }//網(wǎng)絡(luò)請(qǐng)求短信成功后調(diào)用 public void onStart(CountDownTimeView view){ if(send){ if(i==0){ i=60; } handler.postDelayed(runnable,1000); view.setText(i+'s'); send=false; }else { //為了顯示不突兀自動(dòng)減去一秒 view.setText(i-1+'s'); j=i; //將i設(shè)置為0是將所有線程運(yùn)行完畢關(guān)閉釋放內(nèi)存 i=0; handler.removeCallbacksAndMessages(null); //因?yàn)榫€程調(diào)度隨機(jī)性所以要延遲一秒發(fā)送保證子線程關(guān)閉 Message message=Message.obtain(); message.what=3; handler.sendMessageDelayed(message,1000);// i=j; } } //設(shè)置成靜態(tài)保證唯一性 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實(shí)例對(duì)象傳入,并進(jìn)行判斷:
getmessage.setActivity(this); if(getmessage.getI()<60){ if(getmessage.getI()>0){getmessage.onStart(getmessage); }else {getmessage.setText('重新獲取'); } } }
```getmessage是自定義view的實(shí)例對(duì)象。
如要源碼可以到我的git上獲取:
[https://github.com/heybixby/CountDownTimerView```](https://github.com/heybixby/CountDownTimerView)
總結(jié)
到此這篇關(guān)于Android自定義短信倒計(jì)時(shí)view流程分析的文章就介紹到這了,更多相關(guān)android 短信倒計(jì)時(shí)view內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向2. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說(shuō)明3. CSS hack用法案例詳解4. PHP設(shè)計(jì)模式中工廠模式深入詳解5. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)6. ASP+ajax實(shí)現(xiàn)頂一下、踩一下同支持與反對(duì)的實(shí)現(xiàn)代碼7. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析8. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過(guò)程(親測(cè)可用)9. asp中response.write("中文")或者js中文亂碼問(wèn)題10. PHP session反序列化漏洞超詳細(xì)講解
