延时任务的几种创建方式

val TAG: String = "MyActivity"

class MainActivity : AppCompatActivity() {

    private var btn1: Button? = null
    private var btn2: Button? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btn1 = findViewById(R.id.btn1)
        btn2 = findViewById(R.id.btn2)
//        if (!Settings.canDrawOverlays(this)) {
//            val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
//            intent.data = Uri.parse("package:$packageName")
//            startActivityForResult(intent, 0)
//        }
    }

    @RequiresApi(Build.VERSION_CODES.P)
    fun btn1(view: View?) {
        /*
        // demo1 使用handler处理延时任务
        Log.e(TAG, "demo1: handler outer")
        // 创建Handler对象
        val handler = Handler(Looper.myLooper()!!)
        // 发送延时消息
        val token = "AAA"
        handler.postDelayed({
            Log.e(TAG, "btn1: handler runnable")
        }, token,2000)
        // 取消延时任务
        //handler.removeCallbacksAndMessages(token)
        */


        /*
        // 创建CountDownTimer对象
        Log.e(TAG, "demo2: CountDownTimer")
        val intervalTime = 1000L// 倒计时间隔
        val countDownTime = 3000L// 倒计时总时间
        val countDownTimer: CountDownTimer = object : CountDownTimer(countDownTime, intervalTime) {

            override fun onTick(millisUntilFinished: Long) {
                // 倒计时执行中
                Log.e(TAG, "btn1: onTick")
            }

            override fun onFinish() {
                Log.e(TAG, "btn1: onFinish runnable")
                // 倒计时执行完成
            }
        }
        // 开始倒计时
        countDownTimer.start()
        // 取消倒计时
        //countDownTimer.cancel()
        */

        /*
        Log.e(TAG, "demo3: scheduledExecutorService")
        // 创建ScheduledExecutorService对象
        val times = 0
        val scheduledExecutorService = Executors.newScheduledThreadPool(1)
        // 调度延时任务
        val scheduledFuture:ScheduledFuture<*> = scheduledExecutorService.scheduleAtFixedRate({
            // 执行任务
            Log.e(TAG, "btn1: scheduledExecutorService runnable")
        }, 1000,2000, TimeUnit.MILLISECONDS)// 延时1s 间隔2s
        // 取消延时任务
        //scheduledFuture.cancel(true)
        */

        // 创建Timer对象
        Log.e(TAG, "demo4: Timer")
        val timer = Timer()
        // 调度延时任务
        val timerTask: TimerTask = object : TimerTask() {
            override fun run() {
                // 执行任务
                Log.e(TAG, "btn1: TimerTask runnable")
                val launchIntent = Intent(this@MainActivity.applicationContext, MainActivity::class.java)
//                launchIntent.action = Intent.ACTION_MAIN
//                launchIntent.addCategory(Intent.CATEGORY_LAUNCHER)
                launchIntent.flags =
                    Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
                startActivity(launchIntent)
            }
        }
        timer.schedule(timerTask,2000)
        // 取消延时任务
        //timerTask.cancel()

    }

    fun btn2(view: View?) {
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值