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