如何在Android编程中延迟几秒钟执行函数

问题描述

我有两个职能。第一个功能需要8秒钟,第二个功能需要5秒钟。我希望第二个功能以3秒的延迟运行,并且程序不停止且第一个功能不停止。 我怎样才能做到这一点? 请帮助我。

解决方法

使用Handler方法:

 new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    yourfuction(); //This function will only work after 3 second
                }
            },3000);
,
Handler handler =  new Handler();
        Runnable myRunnable = new Runnable() {
            public void run() {
                // Things to be done
                       callYourFunction();
              handler.postDelayed(this,3000);  //after every 3 sec call your function
            }
        };

        handler.postDelayed(myRunnable,3000);