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布局技巧之創(chuàng)建可重用的UI組件
這篇文章主要為大家詳細(xì)介紹了Android布局技巧之創(chuàng)建可重用的UI組件,文中提到了include標(biāo)簽的使用方法,感興趣的小伙伴們可以參考一下2016-05-05
Android編程之Application設(shè)置全局變量及傳值用法實(shí)例分析
這篇文章主要介紹了Android編程之Application設(shè)置全局變量及傳值用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了全局變量及傳值的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12
Android控件之AnalogClock與DigitalClock用法實(shí)例分析
這篇文章主要介紹了Android控件之AnalogClock與DigitalClock用法,以實(shí)例形式分析了Android時(shí)鐘控件AnalogClock和DigitalClock用于顯示時(shí)間的具體使用技巧,需要的朋友可以參考下2015-09-09
Android設(shè)計(jì)模式之單例模式實(shí)例
這篇文章主要介紹了Android設(shè)計(jì)模式之單例模式實(shí)例,單例模式是運(yùn)用最廣泛的設(shè)計(jì)模式之一,在應(yīng)用這個(gè)模式時(shí),單例模式的類必須保證只有一個(gè)實(shí)例存在2023-04-04
Android編程中Perferences的用法實(shí)例分析
這篇文章主要介紹了Android編程中Perferences的用法,以實(shí)例形式較為詳細(xì)的分析了配置文件preferences.xml的功能、定義及使用方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11

