Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

第 8 章 Schedule 计划任务

目录

8.1. 延迟执行
8.2. Time 和 TimerTask 定时刷新
8.3. 使用 Runnable 和 Handler 实现定时执行
8.4. 循环执行
8.5. TimerTask 实现循环播放
8.6. TimerTask 更新 UI

8.1. 延迟执行

		
	new Handler().postDelayed(() -> {
        picture.browsePictureFolder();
    }, 30000);		
		
		
		
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            MainActivity.this.finish();
        }
    }, 1800);		
		
		

Android 11

		
	new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
        public void run() {
            progressBar.setVisibility(View.INVISIBLE);
        }
    }, 3000);		
		
		

循环执行

		
		Handler handler = new Handler(Looper.getMainLooper());
        handler.postDelayed(new Runnable() {
            int count = 5;

            @Override
            public void run() {
                handler.postDelayed(this, 1000);
                runOnUiThread(() -> {
                    binding.textViewAdSkip.setText(String.format("逃过 %d", count));
                });
                count = count - 1;
                if (count == 0) {
                    handler.removeCallbacks(this);
                    binding.frameLayoutAd.setVisibility(View.INVISIBLE);
                    binding.frameLayoutFullscreen.setVisibility(View.VISIBLE);
                }
            }
        }, 1000);