[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)
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
@@ -70,7 +70,7 @@ class DriverMsgBoxBubbleView @JvmOverloads constructor(
|
||||
}
|
||||
|
||||
override fun onDataChanged(category: MsgCategory, msgBoxBean: MsgBoxBean) {
|
||||
UiThreadHandler.post {
|
||||
UiThreadHandler.post({
|
||||
when (category) {
|
||||
MsgCategory.NOTICE -> {
|
||||
MsgBoxConfig.noticeList.add(msgBoxBean)
|
||||
@@ -98,7 +98,7 @@ class DriverMsgBoxBubbleView @JvmOverloads constructor(
|
||||
driverMsgBoxBubbleAdapter?.setData(dataList)
|
||||
}
|
||||
}
|
||||
}
|
||||
}, UiThreadHandler.MODE.QUEUE)
|
||||
}
|
||||
|
||||
override fun onAttachedToWindow() {
|
||||
|
||||
@@ -53,6 +53,18 @@ public final class ThreadUtils {
|
||||
|
||||
private static Executor sDeliver;
|
||||
|
||||
public enum MODE {
|
||||
/**
|
||||
* 多次post同一个Runnable对象,只会执行最后一次
|
||||
*/
|
||||
LATEST,
|
||||
|
||||
/**
|
||||
* 多次post,在主线程消息队列中排队执行
|
||||
*/
|
||||
QUEUE
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether the thread is the main thread.
|
||||
*
|
||||
@@ -63,23 +75,40 @@ public final class ThreadUtils {
|
||||
}
|
||||
|
||||
public static void runOnUiThread(final Runnable r) {
|
||||
runOnUiThread(r, MODE.LATEST);
|
||||
}
|
||||
|
||||
public static void runOnUiThread(final Runnable r, MODE mode) {
|
||||
if (Looper.myLooper() == Looper.getMainLooper()) {
|
||||
r.run();
|
||||
} else {
|
||||
int what = ObjectHashCodeUtils.getHashCodeIfNeed(r);
|
||||
HANDLER.removeMessages(what);
|
||||
Message msg = Message.obtain(HANDLER, r);
|
||||
msg.what = what;
|
||||
HANDLER.sendMessage(msg);
|
||||
if (mode == MODE.LATEST) {
|
||||
int what = ObjectHashCodeUtils.getHashCodeIfNeed(r);
|
||||
HANDLER.removeMessages(what);
|
||||
Message msg = Message.obtain(HANDLER, r);
|
||||
msg.what = what;
|
||||
HANDLER.sendMessage(msg);
|
||||
} else {
|
||||
HANDLER.post(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void runOnUiThreadDelayed(final Runnable r, long delayMillis) {
|
||||
int what = ObjectHashCodeUtils.getHashCodeIfNeed(r);
|
||||
HANDLER.removeMessages(what);
|
||||
Message msg = Message.obtain(HANDLER, r);
|
||||
msg.what = what;
|
||||
HANDLER.sendMessageDelayed(msg, delayMillis);
|
||||
runOnUiThreadDelayed(r, delayMillis, MODE.LATEST);
|
||||
}
|
||||
|
||||
public static void runOnUiThreadDelayed(final Runnable r, long delayMillis, MODE mode) {
|
||||
if (mode == MODE.LATEST) {
|
||||
int what = ObjectHashCodeUtils.getHashCodeIfNeed(r);
|
||||
HANDLER.removeMessages(what);
|
||||
Message msg = Message.obtain(HANDLER, r);
|
||||
msg.what = what;
|
||||
HANDLER.sendMessageDelayed(msg, delayMillis);
|
||||
} else {
|
||||
HANDLER.postDelayed(r, delayMillis);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,24 +10,58 @@ public class UiThreadHandler {
|
||||
|
||||
private UiThreadHandler() { }
|
||||
|
||||
public enum MODE {
|
||||
/**
|
||||
* 多次post同一个Runnable对象,只会执行最后一次
|
||||
*/
|
||||
LATEST,
|
||||
|
||||
/**
|
||||
* 多次post,在主线程消息队列中排队执行
|
||||
*/
|
||||
QUEUE
|
||||
}
|
||||
|
||||
public static boolean post( Runnable r ) {
|
||||
int what = ObjectHashCodeUtils.getHashCodeIfNeed(r);
|
||||
sUiHandler.removeMessages(what);
|
||||
Message msg = Message.obtain(sUiHandler, r);
|
||||
msg.what = what;
|
||||
return sUiHandler.sendMessage(msg);
|
||||
return post(r, MODE.LATEST);
|
||||
}
|
||||
|
||||
public static boolean post(Runnable r, MODE mode) {
|
||||
if (mode == MODE.LATEST) {
|
||||
int what = ObjectHashCodeUtils.getHashCodeIfNeed(r);
|
||||
sUiHandler.removeMessages(what);
|
||||
Message msg = Message.obtain(sUiHandler, r);
|
||||
msg.what = what;
|
||||
return sUiHandler.sendMessage(msg);
|
||||
}
|
||||
return sUiHandler.post(r);
|
||||
}
|
||||
|
||||
public static boolean postDelayed( Runnable r, long delayMillis ) {
|
||||
int what = ObjectHashCodeUtils.getHashCodeIfNeed(r);
|
||||
sUiHandler.removeMessages(what);
|
||||
Message msg = Message.obtain(sUiHandler, r);
|
||||
msg.what = what;
|
||||
return sUiHandler.sendMessageDelayed(msg, delayMillis);
|
||||
return postDelayed(r, delayMillis, MODE.LATEST);
|
||||
}
|
||||
|
||||
public static boolean postDelayed( Runnable r, long delayMillis, MODE mode ) {
|
||||
if (mode == MODE.LATEST) {
|
||||
int what = ObjectHashCodeUtils.getHashCodeIfNeed(r);
|
||||
sUiHandler.removeMessages(what);
|
||||
Message msg = Message.obtain(sUiHandler, r);
|
||||
msg.what = what;
|
||||
return sUiHandler.sendMessageDelayed(msg, delayMillis);
|
||||
}
|
||||
return sUiHandler.postDelayed(r, delayMillis);
|
||||
}
|
||||
|
||||
public static void removeCallbacks( Runnable r ) {
|
||||
int what = ObjectHashCodeUtils.getHashCodeIfNeed(r);
|
||||
sUiHandler.removeMessages(what);
|
||||
removeCallbacks(r, MODE.LATEST);
|
||||
}
|
||||
|
||||
public static void removeCallbacks( Runnable r, MODE mode ) {
|
||||
if (mode == MODE.LATEST) {
|
||||
int what = ObjectHashCodeUtils.getHashCodeIfNeed(r);
|
||||
sUiHandler.removeMessages(what);
|
||||
} else {
|
||||
sUiHandler.removeCallbacks(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user