[2.15.0] 优化UiThreadHandler和ThreadUtils往主线程消息队列添加消息的方式,添加最新和排队两种方式

This commit is contained in:
renwj
2023-04-19 08:57:44 +08:00
parent a7b61aa124
commit 487c07a1be
4 changed files with 152 additions and 89 deletions

View File

@@ -19,70 +19,70 @@ class ANRFix {
companion object {
@JvmStatic
@TargetClass(value = "android.os.Handler", scope = ALL)
@TargetMethod(methodName = "post")
@ReplaceInvoke
fun handlerPostProxy(handler: Handler, action: Runnable): Boolean {
if (Looper.getMainLooper() != handler.looper) {
return handler.post(action)
}
val what = ObjectHashCodeUtils.getHashCodeIfNeed(action)
handler.removeMessages(what)
val msg = Message.obtain(handler, action)
msg.what = what
return handler.sendMessage(msg)
}
@JvmStatic
@TargetClass(value = "android.os.Handler", scope = ALL)
@TargetMethod(methodName = "postDelayed")
@ReplaceInvoke
fun handlerPostDelayProxy(handler: Handler, action: Runnable, delayMillis: Long): Boolean {
if (Looper.getMainLooper() != handler.looper) {
return handler.postDelayed(action, delayMillis)
}
val what = ObjectHashCodeUtils.getHashCodeIfNeed(action)
handler.removeMessages(what)
val msg = Message.obtain(handler, action)
msg.what = what
return handler.sendMessageDelayed(msg, delayMillis)
}
@JvmStatic
@TargetClass(value = "android.app.Activity", scope = ALL)
@TargetMethod(methodName = "post")
@ReplaceInvoke
fun runOnUiThreadOfActivityProxy(activity: Activity, action: Runnable) {
if (Looper.myLooper() != Looper.getMainLooper()) {
UiThreadHandler.post(action)
} else {
activity.runOnUiThread(action)
}
}
@JvmStatic
@TargetClass(value = "android.view.View", scope = ALL)
@TargetMethod(methodName = "post")
@ReplaceInvoke
fun postOfViewProxy(view: View, action: Runnable): Boolean {
return if (Looper.myLooper() != Looper.getMainLooper()) {
UiThreadHandler.post(action)
} else {
view.post(action)
}
}
@JvmStatic
@TargetClass(value = "android.view.View", scope = ALL)
@TargetMethod(methodName = "postDelayed")
@ReplaceInvoke
fun postDelayedOfViewProxy(view: View, action: Runnable, delayMillis: Long): Boolean {
return if (Looper.myLooper() != Looper.getMainLooper()) {
UiThreadHandler.postDelayed(action, delayMillis)
} else {
view.postDelayed(action, delayMillis)
}
}
// @JvmStatic
// @TargetClass(value = "android.os.Handler", scope = ALL)
// @TargetMethod(methodName = "post")
// @ReplaceInvoke
// fun handlerPostProxy(handler: Handler, action: Runnable): Boolean {
// if (Looper.getMainLooper() != handler.looper) {
// return handler.post(action)
// }
// val what = ObjectHashCodeUtils.getHashCodeIfNeed(action)
// handler.removeMessages(what)
// val msg = Message.obtain(handler, action)
// msg.what = what
// return handler.sendMessage(msg)
// }
//
// @JvmStatic
// @TargetClass(value = "android.os.Handler", scope = ALL)
// @TargetMethod(methodName = "postDelayed")
// @ReplaceInvoke
// fun handlerPostDelayProxy(handler: Handler, action: Runnable, delayMillis: Long): Boolean {
// if (Looper.getMainLooper() != handler.looper) {
// return handler.postDelayed(action, delayMillis)
// }
// val what = ObjectHashCodeUtils.getHashCodeIfNeed(action)
// handler.removeMessages(what)
// val msg = Message.obtain(handler, action)
// msg.what = what
// return handler.sendMessageDelayed(msg, delayMillis)
// }
//
// @JvmStatic
// @TargetClass(value = "android.app.Activity", scope = ALL)
// @TargetMethod(methodName = "post")
// @ReplaceInvoke
// fun runOnUiThreadOfActivityProxy(activity: Activity, action: Runnable) {
// if (Looper.myLooper() != Looper.getMainLooper()) {
// UiThreadHandler.post(action)
// } else {
// activity.runOnUiThread(action)
// }
// }
//
// @JvmStatic
// @TargetClass(value = "android.view.View", scope = ALL)
// @TargetMethod(methodName = "post")
// @ReplaceInvoke
// fun postOfViewProxy(view: View, action: Runnable): Boolean {
// return if (Looper.myLooper() != Looper.getMainLooper()) {
// UiThreadHandler.post(action)
// } else {
// view.post(action)
// }
// }
//
// @JvmStatic
// @TargetClass(value = "android.view.View", scope = ALL)
// @TargetMethod(methodName = "postDelayed")
// @ReplaceInvoke
// fun postDelayedOfViewProxy(view: View, action: Runnable, delayMillis: Long): Boolean {
// return if (Looper.myLooper() != Looper.getMainLooper()) {
// UiThreadHandler.postDelayed(action, delayMillis)
// } else {
// view.postDelayed(action, delayMillis)
// }
// }
}
}