Various ways to handle timing in Android

血红的双手。 提交于 2019-12-02 00:34:29

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);

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...

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();

I would use a TimerTask and the method postDelayed. For a good description see here:

http://android-developers.blogspot.com/2007/11/stitch-in-time.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!