Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Android to design a countdown component

Shulou Source: shulou.com Published: 2022-06-03 10:04:16 09月22日 Update

Today, I will talk to you about how to use Android to design a countdown component. Many people may not know much about it. In order to let everyone know more, Xiaobian summarizes the following contents for everyone. I hope everyone can gain something according to this article.

1 Background

We often have countdown scenes in our projects, such as activity countdown, red envelope countdown, etc. Usually, we implement countdown schemes such as CountDownTimer in Android, Timer and ScheduleExcutorService in Java, and interval operator in RxJava. There are two typical problems in the actual project: one is that the implementation form of countdown is not uniform, and the reasons for the non-uniformity are divided into cognitive inconsistency, and each countdown scheme has its own advantages; the other is that there are a large number of countdown simultaneous execution.

2 Comparative analysis

The usage of several schemes is not the focus of this article. Here we list their characteristics in a table. The CountDownTimerManager at the bottom of the table is the freshly baked centralized countdown component that this article will introduce to you.

2.1 Is it a countdown?

The interval operator in Rx sends an event every once in a while. It can be said to be a counter, not a countdown. In actual projects, many students will find that it is used as a countdown. Below is an official RxJava illustration of interval:

interval.png *The Interval operator returns an Observable that emits an infinite sequence of ascending integers, with a constant interval of time of your choosing between emissions. (Simple understanding is to make a callback at fixed intervals)

Through the source code, we can also see that periodic scheduling is actually carried out in ObservableInterval.

public final class ObservableInterval extends Observable { @Override public void subscribeActual(Observer

Tags: Countdown time support hibernation problem task queue delay component design cycle reality situation is in priority scenario timer mobile phone operator method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Linux MariaDB Redmi Huawei