var thread : ThreadClass? = null
inner class ThreadClass:Thread(){
override fun run(){
while(isRunning){
try {
/**시작후 시간 측정*/
when (currentState) {
"시작" -> {
recordCounter++
timeBroadCaster.onNext(recordCounter)
}
"일시정지", "정지" -> {
timeBroadCaster.onNext(recordCounter)
}
}
} catch (e: Exception) {
}
SystemClock.sleep(100)
// Log.d("쓰레드",System.currentTimeMillis().toString())
}
}
}
//쓰레드 초기화
thread = null
isRunning = true
thread = ThreadClass()
thread?.start()
1.
러너블 객체 사용
2. 쓰레드 클래스 사용
/**시작후 시간 측정*/
Thread(Runnable {
threadController = true
while (threadController) {
handlerObject.post {
timeLength = mediaPlayer?.duration?:0 //총길이
val currentPosition = mediaPlayer?.currentPosition //현재위치
try {
timeBroadCaster.onNext(currentPosition!!)
if (timeLength <= currentPosition) {
threadController = false
mediaPlayer?.stop() // 멈춤
mediaPlayer?.release() // 자원 해제
}
}
catch (e: Exception) {
}
}
try {
Thread.sleep(100)
} catch (e: Exception) {
}
}
}).start()
3. 토스트 띄우기
Thread(kotlinx.coroutines.Runnable {
Toast.makeText(
context,
"기본 정보를 설정하는데 실패했습니다. 다시 로그인 해주세요",
Toast.LENGTH_SHORT
).show()
})