It’s a simple Android timing tool, but I haven’t tried it yet.

Time:2022-11-20

Hey! Hello everyone, I haven’t updated for a long time, mainly I don’t know what to write, and I don’t know how other bloggers create, but for me, it’s really difficult to create an original day, but the boss Iron guys, don’t worry, you can’t do daily updates, but you can still do intermittent ones. I also hope that some of my superficial knowledge can bring you some help. Today, I bring you a very A useful timing tool, really easy to use, after using it, you will find that delay and timing have never been so simple.

Before I talk about it, I don’t know how you solve the functions related to timing and delay in normal development. The ancient Handler is still Timer. No matter which way, although we can achieve what we want. effect, but it seems that there is a certain amount of code and life cycle considerations. Is there a way that is simple to call without paying attention to the life cycle?

Anyone who knows the author knows that the former author has been flying in the ocean of the Web for a long time, and he has a deep understanding of the timing and delay in Js. It is so simple. I will post it here for everyone to take a look.

setTimeout(function (){
            //Delay operation after 3 seconds
        },3000)

setInterval(function (){
            //Timing, execute once a second
        },1000)

Delay and timing in Js, veterans, how about it after reading it, simple, in Android, can we also operate like the front end, one word, can, must be able.

By re-encapsulating the ticker in coroutines in Kotlin and using LifecycleOwner to bind the life cycle, a simple timing tool was born. At present, it has been open sourced, and the old irons that need it can be downloaded.

The address is as follows:

https://github.com/AbnerMing8…

Let’s talk about the specific usage method below. Veterans also feel whether the packaging is simple. At present, the remote Maven has been uploaded, and everyone can rely on it.

1. Under the build.gradle file under your root project, import maven.

allprojects {
    repositories {
        maven { url "https://gitee.com/AbnerAndroid/almighty/raw/master" }
    }
}

2. Under the build.gradle file in the Module you need to use, introduce dependencies.

dependencies {
    implementation 'com.vip:time:1.0.0'
}

After the dependency is introduced, we can actually operate it. Come on, let’s have a small test, which is basically similar to the front-end call. For example, a common countdown, we can operate as follows.

setTimeOut(5) {
     // countdown completed
}

You read that right, the call is so simple, the parameter is the time that needs to be counted down, the Long type, is the usage comparable to that of the front end of the web, this method, in the countdown, does not return the countdown time value, and only after the defined time to return.

Some veterans asked, during the countdown, I need to get the returned time value, what should I do? It must be able to do it. You can use the following method. There are two parameters. The first parameter is the countdown or delay time, and the second parameter is the interval time. The default value is 1 second if it is not filled. Whether the pass is equal to end , to determine whether the countdown is complete.

setTimeDown(5) {
    if (it == end) {
       // countdown completed
    } else {
        val t= it.toString()//Countdown time value
    }
}

Similarly, timing is also very simple. For example, if I want to implement an infinite timer, I can use the following method. There is only one parameter, which is the time interval, that is, how many seconds to poll once.

setIntervalWireless(1) {
      val t = it.toString()//time value
   }

Some veterans asked, in actual business, in addition to unlimited timing, there are also some limited timing, such as 1 minute or 10 minutes, how to use timing in such a limited time? It is also very simple, you can use the following method, two parameters, parameter 1 is the time required for timing, parameter 2 is the time interval, how often to poll.

setInterval(10, 2) {
  if (it == end) {
         //timed completion
    } else {
       val t = it.toString()//time value
    }
}

The four methods provided can basically meet the actual business needs. They can be called directly in Activity or Fragment, because they need to be bound with the life cycle. If they are used in View or tool classes, the context can be converted to Activity. Then call it.

This article is not particularly difficult, so let’s stop here. The next article will introduce another packaged library, MVVM’s Base library, so stay tuned!

Recommended Today

Laravel Hprose RPC service

Open source address: https://github.com/flc1125/la… The project supports Laravel and non-Laravel projects, supportsphp >= 5.3version syntax Note:The following content, by default, you already have a certain understanding of Hprose. The content is based on the Laravel project introduction. Install Environment depends on C extensionshprose, see: Hprose official website, Hprose Pecl extension You can install it on […]