Created
August 4, 2019 18:22
-
-
Save championswimmer/33e1fa9e39b52ce101b3c7585f88a4a8 to your computer and use it in GitHub Desktop.
Android Async Handler Difference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codingblocks.handlerasync | |
import android.annotation.SuppressLint | |
import android.os.Bundle | |
import android.os.Handler | |
import android.os.Looper | |
import android.util.Log | |
import androidx.appcompat.app.AppCompatActivity | |
import kotlinx.android.synthetic.main.activity_main.* | |
class MainActivity : AppCompatActivity() { | |
@SuppressLint("NewApi") | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
val start = System.currentTimeMillis() | |
var curr = start | |
var delta = 0L | |
var delay = 0L | |
val h = Handler.createAsync(Looper.myLooper()) | |
// val h = Handler() | |
repeat(100) { i -> | |
h.postDelayed({ | |
delta = System.currentTimeMillis() - curr | |
curr += delta | |
delay = curr - start | |
Log.d( | |
"TIME", | |
"iteration = $i, delay = $delay, delta = $delta text = ${myTextView.text}" | |
) | |
}, i * 5L) | |
} | |
repeat(100) { i -> | |
h.postDelayed({ | |
myTextView.text = delay.toString() | |
}, i * 5L) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment