[2.15.0] 优化UiThreadHandler和ThreadUtils往主线程消息队列添加消息的方式,添加最新和排队两种方式
This commit is contained in:
@@ -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)
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user