Android移动开发,Handle使用

handler可以看做是一个事件分发器,可以立即执行,也可以延时执行(小白理解)

package com.ydduong.gsa.view

class A {

    // 1.标识码
    private val msgAudioPrepared = 0x111
    private val msgVoiceChanged = 0x112
    private val msgDialogDismiss = 0x113
    private val msgRecorderKeyWord = 0x114
    // 2.handler
    private val handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when (msg.what) {
                msgAudioPrepared -> {
                    
                }
                msgVoiceChanged -> {
                    
                }
                msgDialogDismiss -> {
                    
                }
                msgRecorderKeyWord -> {
                    
                }
            }
        }
    }

	// 3.使用
	fun useHandler() {
		handler.sendEmptyMessageDelayed(msgDialogDismiss, 1300) // 1.3秒之后执行
		handler.sendEmptyMessage(msgAudioPrepared) // 立即执行
	}
}

你可能感兴趣的:(Android)