偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Android倒計(jì)時(shí)實(shí)現(xiàn)方式:五種方式對(duì)比解析

移動(dòng)開發(fā) Android
無論選擇哪種方案,都要特別注意生命周期管理和內(nèi)存泄漏預(yù)防,建議在??ViewModel??中處理倒計(jì)時(shí)邏輯,通過??LiveData??更新界面。

在驗(yàn)證碼發(fā)送、秒殺活動(dòng)、運(yùn)動(dòng)計(jì)時(shí)等場(chǎng)景中,倒計(jì)時(shí)功能的身影隨處可見。本文從傳統(tǒng)Handler到現(xiàn)代協(xié)程Flow,對(duì)比5種倒計(jì)時(shí)實(shí)現(xiàn)方案!

1?? Handler消息機(jī)制實(shí)現(xiàn)

class CountdownActivity : AppCompatActivity() {

    private var remainingSeconds = 60
    private lateinit var countdownHandler: Handler
    
    private val countdownRunnable = object : Runnable {
        override fun run() {
            if (remainingSeconds > 0) {
                binding.timerText.text = "${remainingSeconds--}s"
                countdownHandler.postDelayed(this, 1000)
            } else {
                binding.timerText.text = "時(shí)間到!"
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityCountdownBinding.inflate(layoutInflater)
        countdownHandler = Handler(Looper.getMainLooper())
        
        binding.startButton.setOnClickListener {
            remainingSeconds = 60
            countdownHandler.post(countdownRunnable)
        }
    }

    override fun onStop() {
        super.onStop()
        countdownHandler.removeCallbacks(countdownRunnable)
    }
}

實(shí)現(xiàn)要點(diǎn)

? 使用主線程Looper創(chuàng)建Handler

? 通過postDelayed實(shí)現(xiàn)秒級(jí)延遲

? 在頁面不可見時(shí)及時(shí)移除回調(diào)

? 注意:連續(xù)點(diǎn)擊可能造成多個(gè)倒計(jì)時(shí)并行

2?? 系統(tǒng)CountDownTimer工具類

val countDown = object : CountDownTimer(30000, 1000) {
    override fun onTick(millisUntilFinished: Long) {
        binding.progressBar.progress = (millisUntilFinished / 1000).toInt()
        binding.timeText.text = "剩余 ${millisUntilFinished / 1000} 秒"
    }

    override fun onFinish() {
        binding.timeText.text = "倒計(jì)時(shí)結(jié)束"
        binding.progressBar.progress = 0
    }
}

binding.startButton.setOnClickListener {
    countDown.start()
}

binding.cancelButton.setOnClickListener {
    countDown.cancel()
}

優(yōu)勢(shì)分析

? 官方封裝好的倒計(jì)時(shí)組件

? 支持倒計(jì)時(shí)進(jìn)度同步更新

? 提供完成回調(diào)接口

3?? Timer定時(shí)任務(wù)方案

private var timer: Timer? = null

fun startCountdown(duration: Int) {
    timer?.cancel()
    timer = Timer().apply {
        schedule(object : TimerTask() {
            var current = duration
            override fun run() {
                runOnUiThread {
                    when {
                        current > 0 -> {
                            binding.statusText.text = "剩余${current--}秒"
                        }
                        else -> {
                            binding.statusText.text = "已完成"
                            cancel()
                        }
                    }
                }
            }
        }, 0, 1000)
    }
}

override fun onDestroy() {
    timer?.cancel()
    super.onDestroy()
}

注意事項(xiàng)

? 通過UI線程更新界面

TimerTask在子線程執(zhí)行

? 及時(shí)cancel避免內(nèi)存泄漏

? 不支持暫停/恢復(fù)功能

4?? RxJava響應(yīng)式實(shí)現(xiàn)

private var disposable: Disposable? = null

fun rxjavaCountdown(total: Int) {
    disposable = Observable.interval(0, 1, TimeUnit.SECONDS)
        .take(total.toLong())
        .map { total - it.toInt() }
        .observeOn(AndroidSchedulers.mainThread())
        .doOnSubscribe { binding.progressBar.max = total }
        .subscribe(
            { remaining ->
                binding.progressBar.progress = remaining
                binding.countText.text = "$remaining"
            },
            { error -> showError(error) },
            { showCompletion() }
        )
}

fun stopCountdown() {
    disposable?.dispose()
}

適用場(chǎng)景

? 需要與其他Rx操作符配合

? 存在多個(gè)倒計(jì)時(shí)任務(wù)

? 需要線程切換控制

? 支持錯(cuò)誤處理回調(diào)

5?? Kotlin Flow協(xié)程方案

private var countdownJob: Job? = null

fun flowCountdown(total: Int) {
    countdownJob?.cancel()
    countdownJob = lifecycleScope.launch {
        flow {
            for (i in total downTo 0) {
                delay(1000)
                emit(i)
            }
        }.onStart { 
            showLoading() 
        }.onEach { remaining ->
            withContext(Dispatchers.Main) {
                updateUI(remaining)
            }
        }.onCompletion { 
            showResult() 
        }.catch { 
            handleError(it) 
        }.collect()
    }
}

fun cancelFlow() {
    countdownJob?.cancel()
}

現(xiàn)代特性

? 結(jié)構(gòu)化并發(fā)管理

? 關(guān)聯(lián)生命周期感知

? 支持異步異常處理

? 可組合的流操作符

? 自動(dòng)取消協(xié)程任務(wù)

?? 方案對(duì)比表

特性

Handler

CountDownTimer

Timer

RxJava

Flow

線程安全

需處理

?

?

?

?

生命周期感知

?

?

?

需配置

?

內(nèi)存泄漏風(fēng)險(xiǎn)

暫停/恢復(fù)功能

需實(shí)現(xiàn)

?

需實(shí)現(xiàn)

需實(shí)現(xiàn)

易實(shí)現(xiàn)

錯(cuò)誤處理機(jī)制

?

?

?

?

?

代碼復(fù)雜度

推薦使用場(chǎng)景

簡(jiǎn)單場(chǎng)景

基礎(chǔ)倒計(jì)時(shí)

后臺(tái)任務(wù)

復(fù)雜邏輯

現(xiàn)代架構(gòu)

1. 簡(jiǎn)單需求:優(yōu)先選用CountDownTimer,避免重復(fù)造輪子

2. 界面交互:使用Handler時(shí)注意與View的生命周期綁定

3. 后臺(tái)任務(wù):Timer方案需配合Service使用

4. 響應(yīng)式開發(fā):已有RxJava項(xiàng)目可繼續(xù)使用倒計(jì)時(shí)操作符

5. 新項(xiàng)目推薦:采用Kotlin Flow實(shí)現(xiàn),搭配協(xié)程更高效

6. 性能關(guān)鍵:避免在倒計(jì)時(shí)回調(diào)中執(zhí)行耗時(shí)操作

7. 內(nèi)存優(yōu)化:所有方案都需注意釋放資源

?? 注意事項(xiàng):無論選擇哪種方案,都要特別注意生命周期管理和內(nèi)存泄漏預(yù)防,建議在ViewModel中處理倒計(jì)時(shí)邏輯,通過LiveData更新界面。

責(zé)任編輯:武曉燕 來源: 沐雨花飛碟
相關(guān)推薦

2014-08-18 14:30:27

Android倒計(jì)時(shí)

2022-10-21 15:42:21

倒計(jì)時(shí)鴻蒙

2025-04-11 08:42:37

JavaScript倒計(jì)時(shí)前端

2016-12-07 10:02:54

移動(dòng)應(yīng)用開發(fā)底部導(dǎo)航android

2013-10-10 09:23:15

Android 4.4Kitkat

2014-03-21 13:46:45

2017-07-20 16:21:52

UICountDownTidelay

2011-04-11 09:17:28

Ubuntu倒計(jì)時(shí)

2015-03-23 17:58:04

驗(yàn)證碼倒計(jì)時(shí)并行

2015-01-21 16:07:57

Android源碼驗(yàn)證碼倒計(jì)時(shí)

2011-04-11 09:50:56

Ubuntu 11.0

2014-02-18 10:36:33

2013-03-11 09:03:30

云計(jì)算

2010-01-25 11:29:33

Android計(jì)時(shí)器

2025-02-13 07:45:26

APISpringHTTP

2010-08-13 13:25:53

Flex頁面跳轉(zhuǎn)

2022-06-14 08:45:27

瀏覽器IEWindows

2013-10-08 09:24:39

Windows 8.1Windows 8

2013-04-09 10:01:18

微軟Windows XP

2020-10-28 17:54:49

成都信息安全
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)