-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathBaseReceiver.java
More file actions
132 lines (120 loc) · 3.94 KB
/
BaseReceiver.java
File metadata and controls
132 lines (120 loc) · 3.94 KB
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
package common.base;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.text.TextUtils;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import java.util.HashSet;
/**
* ******************(^_^)***********************<br>
* User: 11776610771@qq.com<br>
* Date: 2018/6/2<br>
* Time: 12:40<br>
* <P>DESC:
* 广播接收者的通用类,可批量给该接收者添加要接收的对应的actions
* </p>
* ******************(^_^)***********************
*/
public abstract class BaseReceiver<I extends BaseReceiver<I>> extends BroadcastReceiver {
protected final String TAG = getClass().getSimpleName();
/**
* 本广播接收者基类,只有一个IntentFilter,但广播接收者是可以有多个IntentFilter的
*/
protected IntentFilter mIntentFilter;
protected boolean asLocalReceiver;
public I withAction(String action,String dataType) {
if (action != null) {
if (mIntentFilter == null) {
mIntentFilter = new IntentFilter();
}
mIntentFilter.addAction(action);
if (!TextUtils.isEmpty(dataType)) {
try {
mIntentFilter.addDataType(dataType);
} catch (IntentFilter.MalformedMimeTypeException e) {
e.printStackTrace();
}
}
}
return self();
}
public I withAction(String action) {
return withAction(action, null);
}
public I withActions(String... actions) {
if (actions != null && actions.length > 0) {
for (String theAction : actions) {
withAction(theAction, null);
}
}
return self();
}
/**
* 将本广播接收者注册
* @param context Context
* @param registerAsLocal 是否作为本地广播接收者注册.
*/
public void register(Context context, boolean registerAsLocal) {
if (registerAsLocal) {
LocalBroadcastManager.getInstance(context).registerReceiver(this,mIntentFilter);
}
else{
context.registerReceiver(this, mIntentFilter);
}
}
public void register(Context context) {
register(context,false);
}
public void unRegister(Context context) {
context.unregisterReceiver(this);
}
/**
* 解注册本广播接收者
* @param context Context
* @param unRegisterInLocal 是否解注册本广播为本地广播
*/
public void unRegister(Context context, boolean unRegisterInLocal) {
if (unRegisterInLocal) {
LocalBroadcastManager.getInstance(context).unregisterReceiver(this);
}
else{
context.unregisterReceiver(this);
}
}
public IntentFilter getInnerIntentFilter() {
return mIntentFilter;
}
protected final I self() {
return (I) this;
}
protected HashSet<String> ignoreActions;
/**
* 添加要忽略的Action
* @param willIgnoreActions 将要忽略的广播Action
*/
public void ignoreActions(String... willIgnoreActions) {
if (willIgnoreActions != null && willIgnoreActions.length > 0) {
if (ignoreActions == null) {
ignoreActions = new HashSet<>();
}
for (String willIgnoreAction : willIgnoreActions) {
if (ignoreActions.contains(willIgnoreAction)) {
continue;
}
ignoreActions.add(willIgnoreAction);
}
}
}
public boolean isActionIgnored(String theAction) {
return ignoreActions != null && ignoreActions.contains(theAction);
}
// public static void main(String[] args) {
// HashSet<String> a = new HashSet<>(3);
// a.add("a");
// a.add("a");
// a.add("c");
// for (String s : a) {
// System.out.println(s);
// }
// }
}