日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android?使用flow實現倒計時的方式_Android

作者:i小灰 ? 更新時間: 2022-06-21 編程語言

Android 倒計時一般實現方式:

  • handler+postDelayed() 方式
  • Timer + TimerTask + handler 方式
  • ScheduledExecutorService + handler 方式
  • RxJava 方式
  • CountDownTimer 方式

現在因為有了協程和Flow,我們可以借助Flow這個工具,更加優雅地實現這個需求功能.

1.依賴導入

    api 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2'
    api 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'
    // lifecycleScope(可選)
    api "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0"

2. 代碼實現

fun countDownCoroutines(
    total: Int,
    scope: CoroutineScope,
    onTick: (Int) -> Unit,
    onStart: (() -> Unit)? = null,
    onFinish: (() -> Unit)? = null,
): Job {
    return flow {
        for (i in total downTo 0) {
            emit(i)
            delay(1000)
        }
    }.flowOn(Dispatchers.Main)
        .onStart { onStart?.invoke() }
        .onCompletion { onFinish?.invoke() }
        .onEach { onTick.invoke(it) }
        .launchIn(scope)
}

2.1使用:

private var mCountdownJob: Job? = null

mBinding.btnStart.setOnClickListener {
    mCountdownJob = countDownCoroutines(60, lifecycleScope,
        onTick = { second ->
            mBinding.text.text = "${second}s后重發"
        }, onStart = {
            // 倒計時開始
        }, onFinish = {
            // 倒計時結束,重置狀態
            mBinding.text.text = "發送驗證碼"
        })
}
mBinding.btnStop.setOnClickListener { 
    // 取消倒計時
    mCountdownJob?.cancel() 

其他的完整Demo https://github.com/dahui888/kotlinpractice

補充:

下面是小編收集整理Android 實現倒計時的幾種方式

使用 Timer方式:


? /**
? ? ?* 開始
? ? ?*/
? ? public void startTimer() {
? ? ? ? if (timer == null) {
? ? ? ? ? ? timer = new Timer();
? ? ? ? }
? ? ? ? if (timerTask == null) {
? ? ? ? ? ? timerTask = new TimerTask() {
? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? ? ? Message message = new Message();
? ? ? ? ? ? ? ? ? ? message.what = 2;
? ? ? ? ? ? ? ? ? ? handler.sendMessage(message);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? };
? ? ? ? }
? ? ? ? if (timer != null && timerTask != null) {
? ? ? ? ? ? timer.schedule(timerTask, 0, 2000);
? ? ? ? }
? ? }
? ? /**
? ? ?* 暫停定時器
? ? ?*/
? ? public void stopTimer() {
? ? ? ? if (timer != null) {
? ? ? ? ? ? timer.cancel();
? ? ? ? ? ? timer = null;
? ? ? ? }
? ? ? ? if (timerTask != null) {
? ? ? ? ? ? timerTask.cancel();
? ? ? ? ? ? timerTask = null;
? ? ? ? }
? ? }

使用rxjava方式:

 ?private void countDown() {
? ? ? ? mdDisposable = Flowable.intervalRange(0, Constant.COUNT_DOWN, 0, ? ? ? ? ? ? ? ? ? ? 1,TimeUnit.SECONDS)
? ? ? ? ? ? ? ? .observeOn(AndroidSchedulers.mainThread())
? ? ? ? ? ? ? ? .doOnNext((aLong) -> LogUtils.e("倒計時--" + aLong))
? ? ? ? ? ? ? ? .doOnComplete(() -> randomSelectSeat())
? ? ? ? ? ? ? ? .subscribe();
? ? }
? ? /**
? ? ?* 銷毀
? ? ?*/
? ? ?@Override
? ? protected void onDestroy() {
? ? ? ? if (mdDisposable != null) {
? ? ? ? ? ? mdDisposable.dispose();
? ? ? ? }
? ? ? ? super.onDestroy();
? ? }

使用CountDownTimer方式:

//倒計時CountDownTimer
//每過1000毫秒執行一次onTick
//倒計時完成執行onFinish
CountDownTimer timer = new CountDownTimer(5000, 1000){
? ? @Override
? ? public void onTick(long sin) {
? ? ? ? Toast.makeText(MainActivity.this, "" + sin/1000, Toast.LENGTH_SHORT).show();
? ? }
?
? ? @Override
? ? public void onFinish() {
? ? ? ? Toast.makeText(MainActivity.this, "倒計時完成", Toast.LENGTH_SHORT).show();
? ? }
};
//開始
timer.start();
//暫停
if (timer != null) {
? ? ?timer.cancel();
? ? ?timer = null;
?}

原文鏈接:https://juejin.cn/post/7011018975333056519

相關推薦

欄目分類
最近更新