美文网首页
MainLooperWatcher

MainLooperWatcher

作者: gcp | 来源:发表于2023-04-19 12:51 被阅读0次

    package com.iflytek.autofly.mediax

    import android.os.Debug

    import android.os.Looper

    import android.os.SystemClock

    import android.text.TextUtils

    import android.util.Log

    import android.util.Printer

    import android.util.StringBuilderPrinter

    // watch all runnable which post in main thread

    class MainLooperWatcher : Printer {

    private val TAG ="MainLooperWatcher"

        private var mLastMillis: Long =0

        fun mainThreadWatcher() {

    Looper.getMainLooper().setMessageLogging(this)

    }

    override fun println(msg: String?) {

    // ignore system task

            if (msg ==null

                    || TextUtils.isEmpty(msg)

    ||msg.contains("Choreographer")

    ||msg.contains("ActivityThread\$H")

    )return

            if (msg.startsWith(">>>>> Dispatching")) {

    mLastMillis = SystemClock.elapsedRealtime()

    }else if (msg.startsWith("<<<<< Finished")) {

    val now = SystemClock.elapsedRealtime()

    val usedMillis = now -mLastMillis

                if (usedMillis <16L) {

    return

                }

    if (usedMillis <100L) {

    Log.w(TAG, " $usedMillis ms used for $msg")

    return

                }

    if (usedMillis >=100L) {

    report(msg, usedMillis)

    }

    }

    }

    private fun report(msg: String, usedMillis: Long) {

    val sb = StringBuilder()

    Looper.getMainLooper().dump(StringBuilderPrinter(sb), "")

    Log.e(TAG, "$usedMillis ms used for $msg, $sb")

    val exp ="[Block Runnable] ${usedMillis}ms used for ${msg}"

            Log.e(TAG, exp)

    if (Debug.isDebuggerConnected() || Debug.waitingForDebugger()){

    return

            }

    if (BuildConfig.DEBUG && usedMillis >2000) {

    throw RuntimeException(exp)

    }

    }

    }

    相关文章

      网友评论

          本文标题:MainLooperWatcher

          本文链接:https://www.haomeiwen.com/subject/vrhljdtx.html