不同的方式来处理定时的Andr​​oid [英] Various ways to handle timing in Android

查看:106
本文介绍了不同的方式来处理定时的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个非常简单的方法来记录时间。基本上,我只需要启动时调用一个方法调用另一个方法的计时器,和时间(45-90秒)后一组量。

I have a need for a very simplistic way to keep track of time. Basically I simply need to start a timer when a method is called, and a set amount of time after that (45-90 seconds) call another method.

从我已阅读,处理程序是最有效的方式来处理跟踪时间。然而,有些人说他们需要一个第二个线程,而另一些人说,他们不知道。这当然不是一个大问题,但我会preFER让事情简约越好。那么,有没有办法做这样的事情只是检查是否System.getCurrentTimeMillis一定量高于它,当用户调用的第一个方法,当然没有任何进一步的用户交互。

From what I have read, Handlers are the most efficient way to handle keeping track of time. However, some say they require a second thread, while others say they don't. It's certainly not a huge deal, but I would prefer to keep things as minimalistic as possible. So, is there any way to do something like simply checking to see if System.getCurrentTimeMillis is a certain amount higher than it was when the user called the first method, without any further user interaction of course.

如果不是我只是读入处理程序更多的工作与这些。感谢您的帮助。

If not I will just read into Handlers more and work with those. Thank you for your help.

推荐答案

在你打电话给你的第一个方法90秒与后调用第二种方法:

After you call your first method you call the second method after 90 seconds with:

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            //here call the second method

            }   

}, 90000);

然而,如果用户点击返回和离开活动,这可运行postdelayed将重新启动的活性,因此,需要把一个如果它语句,以检查是否该活动仍在运行...

However, if the user clicks on return and leave the activity, this postdelayed runnable will restart the activity, hence you need to put an if statement in it to check if that activity is still running...

如果你需要监视多少时间已经过去了每隔1秒计时器结束之前,那么你可以使用这个code

If you need to monitor how much time has passed every 1 second before the timer has ended then you can use this code

new CountDownTimer(90000, 1000) { 

    public void onTick(long millisUntilFinished) { 
        //call to my UI thread every one second 
    } 

    public void onFinish() { 
        //final call to my UI thread after 90 seconds
    } 
 }.start();

这篇关于不同的方式来处理定时的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆