EventBus.java
1018 Bytes
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
package com.sdk;
import java.util.ArrayList;
import java.util.HashMap;
public class EventBus {
public static int PHONE_COMING = 0;
public static int SCREEN_ON = 1;
public static EventBus sInstance = new EventBus();
private HashMap<Integer,ArrayList<Listener>> listHashMap = new HashMap<>();
public static EventBus getInstance(){
return sInstance;
}
private EventBus(){
listHashMap.put(PHONE_COMING,new ArrayList<Listener>());
listHashMap.put(SCREEN_ON,new ArrayList<Listener>());
}
public synchronized void register(int type,Listener listener){
listHashMap.get(type).add(listener);
}
public synchronized void notify(int type){
for(Listener listener:listHashMap.get(type)){
listener.onEvent(type);
}
}
public synchronized void unregister(int type,Listener listener){
listHashMap.get(type).remove(listener);
}
public interface Listener{
void onEvent(int notify);
}
}