Android?使用flow實(shí)現(xiàn)倒計(jì)時(shí)的方式
Android 倒計(jì)時(shí)一般實(shí)現(xiàn)方式:
- handler+postDelayed() 方式
- Timer + TimerTask + handler 方式
- ScheduledExecutorService + handler 方式
- RxJava 方式
- CountDownTimer 方式
現(xiàn)在因?yàn)橛辛藚f(xié)程和Flow,我們可以借助Flow這個(gè)工具,更加優(yōu)雅地實(shí)現(xiàn)這個(gè)需求功能.
1.依賴導(dǎo)入
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. 代碼實(shí)現(xiàn)
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后重發(fā)" }, onStart = { // 倒計(jì)時(shí)開始 }, onFinish = { // 倒計(jì)時(shí)結(jié)束,重置狀態(tài) mBinding.text.text = "發(fā)送驗(yàn)證碼" }) } mBinding.btnStop.setOnClickListener { // 取消倒計(jì)時(shí) mCountdownJob?.cancel()
其他的完整Demo https://github.com/dahui888/kotlinpractice
補(bǔ)充:
下面是小編收集整理Android 實(shí)現(xiàn)倒計(jì)時(shí)的幾種方式
使用 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); ? ? ? ? } ? ? } ? ? /** ? ? ?* 暫停定時(shí)器 ? ? ?*/ ? ? 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("倒計(jì)時(shí)--" + aLong)) ? ? ? ? ? ? ? ? .doOnComplete(() -> randomSelectSeat()) ? ? ? ? ? ? ? ? .subscribe(); ? ? } ? ? /** ? ? ?* 銷毀 ? ? ?*/ ? ? ?@Override ? ? protected void onDestroy() { ? ? ? ? if (mdDisposable != null) { ? ? ? ? ? ? mdDisposable.dispose(); ? ? ? ? } ? ? ? ? super.onDestroy(); ? ? }
使用CountDownTimer方式:
//倒計(jì)時(shí)CountDownTimer //每過1000毫秒執(zhí)行一次onTick //倒計(jì)時(shí)完成執(zhí)行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, "倒計(jì)時(shí)完成", Toast.LENGTH_SHORT).show(); ? ? } }; //開始 timer.start(); //暫停 if (timer != null) { ? ? ?timer.cancel(); ? ? ?timer = null; ?}
到此這篇關(guān)于Android 使用flow實(shí)現(xiàn)倒計(jì)時(shí)的方式的文章就介紹到這了,更多相關(guān)android flow倒計(jì)時(shí)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android實(shí)現(xiàn)圓圈倒計(jì)時(shí)
- Android實(shí)現(xiàn)倒計(jì)時(shí)的方案梳理
- Android實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)自定義控件
- Android自定義View實(shí)現(xiàn)隨機(jī)數(shù)驗(yàn)證碼
- Android自定義驗(yàn)證碼輸入框的方法實(shí)例
- Android實(shí)現(xiàn)短信驗(yàn)證碼輸入框
- Android滑動(dòng)拼圖驗(yàn)證碼控件使用方法詳解
- Android實(shí)現(xiàn)隨機(jī)生成驗(yàn)證碼
- OpenHarmony實(shí)現(xiàn)類Android短信驗(yàn)證碼及倒計(jì)時(shí)流程詳解
相關(guān)文章
Android刮刮卡實(shí)現(xiàn)原理與代碼講解
這篇文章主要為大家詳細(xì)介紹了Android刮刮卡實(shí)現(xiàn)原理、實(shí)現(xiàn)原理步驟以及代碼講解,感興趣的小伙伴們可以參考一下2016-04-04Android實(shí)現(xiàn)一對(duì)一藍(lán)牙聊天APP
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)一對(duì)一藍(lán)牙聊天APP,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06解決webview 第二次調(diào)用loadUrl頁面不刷新的問題
這篇文章主要介紹了解決webview 第二次調(diào)用loadUrl頁面不刷新的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android 老生常談LayoutInflater的新認(rèn)知
今天不想去聊一些Android的新功能,新特性之類的東西,特別想聊一聊這個(gè)老生常談的話題:LayoutInflater,感興趣的朋友來看看吧2022-03-03Android數(shù)據(jù)庫LitePal的基本用法詳解
這篇文章主要介紹了Android數(shù)據(jù)庫LitePal的基本用法詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01