-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommonHandlerThread.java
204 lines (179 loc) · 4.01 KB
/
CommonHandlerThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package com.baidu.tuke_demo;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* 通用HandlerThread工具,提供简单业务队列的子线程实现,避免多线程互斥的使用
* @author tuke
*/
public class CommonHandlerThread {
private static final String TAG = CommonHandlerThread.class.getSimpleName();
private static CommonHandlerThread sInstance = null;
//默认消息 可以定义更多
public static final int MSG_ID_DEFAULT = 1;
//消息2
public static final int MSG_ID_DEFAULT_2 = 2;
/**
* 线程Handler
*/
private HandlerThread mHandlerThread = null;
/**
* 处理消息的Handler
*/
private Handler mHandler = null;
/**
* 回调集合
*/
private List<Callback> mCallbacks = new ArrayList<Callback>();
protected CommonHandlerThread() {
//初始化
init();
}
//单例
public static CommonHandlerThread getInstance() {
if ( sInstance == null ) {
synchronized (CommonHandlerThread.class) {
if ( sInstance == null ) {
sInstance = new CommonHandlerThread();
}
}
}
return sInstance;
}
public Handler getHandler() {
return mHandler;
}
public Looper getLooper() {
if ( mHandlerThread == null ) {
return null;
}
return mHandlerThread.getLooper();
}
/**
* 注册回调
* @param cb
*/
public void registerCallback(Callback cb) {
if ( cb != null && !mCallbacks.contains(cb )) {
cb.careAbouts();
mCallbacks.add(cb);
}
}
/**
* 移除回调
* @param cb
*/
public void unregisterCallback(Callback cb) {
if ( cb != null && mCallbacks.contains(cb)) {
mCallbacks.remove(cb);
}
}
/**
* 移除消息
* @param what
*/
public void removeMessage(int what) {
if ( mHandler != null && mHandler.hasMessages(what)) {
mHandler.removeMessages(what);
}
}
/**
* 发送消息
* @param what
* @return
*/
public boolean sendMessage(int what) {
return sendMessage(what, 0, 0, null, 0);
}
/**
* 发送消息
* @param what
* @param arg1
* @param arg2
* @param obj
* @param delayTime
* @return
*/
public boolean sendMessage(int what, int arg1, int arg2, Object obj, long delayTime) {
if ( mHandler == null ) {
return false;
}
Message message = mHandler.obtainMessage(what);
message.arg1 = arg1;
message.arg2 = arg2;
if ( obj != null ) {
message.obj = obj;
}
if ( delayTime <= 0 ) {
mHandler.sendMessage(message);
} else {
mHandler.sendMessageDelayed(message, delayTime);
}
return true;
}
/**
* 回调逻辑执行类
*
* */
public static abstract class Callback {
//关注的消息ID
private Set<Integer> mCareMsgs = new HashSet<Integer>();
/**
*
* 注册关注的消息,请调用 careAbout(msgID)方法关注
* */
public abstract void careAbouts();
/**
* 执行
* */
public abstract void execute(Message message);
/**
* 注册单个关注的消息
* */
public final void careAbout(int msgID) {
mCareMsgs.add(msgID);
}
/**
* 检测是否关系指定消息
* */
public final boolean isCareAbout(int msgID) {
return mCareMsgs.contains(msgID);
}
/**
* 一个标识名
* */
public String getName() {
return "default";
}
}
/**
* 初始化
*/
private void init() {
if (mHandlerThread != null ) {
return;
}
mHandlerThread = new HandlerThread("CommonHandlerThread");
mHandlerThread.start();
mHandler = new Handler(mHandlerThread.getLooper()) {
@Override
public void handleMessage(Message msg) {
//线程中处理消息
//遍历回调消息集合
for (int i = mCallbacks.size() - 1 ; i >= 0 && i < mCallbacks.size() && mCallbacks.get(i) != null ; i-- ) {
//如果关注多个消息 消息是串行处理的
CommonHandlerThread.Callback callback = mCallbacks.get(i);
//如果是关注的消息
if (callback != null && callback.isCareAbout(msg.what)) {
callback.execute(msg);
}
}
}
};
}
}