Skip to content

Instantly share code, notes, and snippets.

@TomerPacific
Created April 28, 2022 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TomerPacific/08fb9cfe7f74282655e99e6bc8ea9b7d to your computer and use it in GitHub Desktop.
Save TomerPacific/08fb9cfe7f74282655e99e6bc8ea9b7d to your computer and use it in GitHub Desktop.
ANR Thread
package com.tomerpacific.anrdetection
import android.os.Handler
import android.os.Looper
import java.lang.Exception
class ANRHandler: Thread() {
val TIMEOUT: Long = 5000L
private val handler: Handler = Handler(Looper.getMainLooper())
private val worker : Runnable = Runnable { }
override fun run() {
while (!isInterrupted) {
handler.postAtFrontOfQueue(worker)
try {
sleep(TIMEOUT)
} catch (exception: Exception) {
exception.printStackTrace()
}
if (handler.hasMessages(0)) {
//worker has not finished running so the UI thread is being held
val stackTrace: Array<StackTraceElement> = currentThread().stackTrace
var output: String = ""
for (element in stackTrace) {
output += element.className + " " + element.methodName + " " + element.lineNumber
}
print(output)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment