且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

等待时间 - 机器人

更新时间:2022-05-03 08:25:00

取决于什么线程你想睡觉。你也可以把你的方法,在一个单独的线程,做你的方法存在。这样,你的应用程序将不会挂起/睡眠

Depends what thread your trying to sleep. You can also put your method in a seperate thread and do your methods there. This way your app will not hang/sleep

private class TimeoutOperation extends AsyncTask<String, Void, Void>{

    @Override
    protected Void doInBackground(String... params) {

        try {
            Log.i(TAG, "Going to sleep");
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        Log.i(TAG, "This is executed after 10 seconds and runs on the main thread");
        //Update your layout here
        super.onPostExecute(result);
    }
}

要执行此操作使用

new TimeoutOperation().execute("");