-
-
Save TomerPacific/08fb9cfe7f74282655e99e6bc8ea9b7d to your computer and use it in GitHub Desktop.
ANR Thread
This file contains 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.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