Skip to content

Commit decc0f2

Browse files
committed
增加localserviceAPI
1 parent 2b00a57 commit decc0f2

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.plugin.core.localservice;
2+
3+
/**
4+
* Created by cailiming on 16/1/1.
5+
*
6+
* 所有context公用
7+
*
8+
*/
9+
public abstract class LocalServiceFetcher {
10+
int mServiceId;
11+
private Object mCachedInstance;
12+
13+
public final Object getService() {
14+
synchronized (LocalServiceFetcher.this) {
15+
Object service = mCachedInstance;
16+
if (service != null) {
17+
return service;
18+
}
19+
return mCachedInstance = createService(mServiceId);
20+
}
21+
}
22+
23+
public abstract Object createService(int serviceId);
24+
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.plugin.core.localservice;
2+
3+
import java.util.HashMap;
4+
5+
/**
6+
* Created by cailiming on 16/1/1.
7+
*/
8+
public class LocalServiceManager {
9+
10+
private static final HashMap<String, LocalServiceFetcher> SYSTEM_SERVICE_MAP =
11+
new HashMap<String, LocalServiceFetcher>();
12+
13+
private LocalServiceManager() {
14+
}
15+
16+
public static void registerService(String serviceName, LocalServiceFetcher fetcher) {
17+
fetcher.mServiceId ++;
18+
SYSTEM_SERVICE_MAP.put(serviceName, fetcher);
19+
}
20+
21+
public static Object getService(String name) {
22+
LocalServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name);
23+
return fetcher == null ? null : fetcher.getService();
24+
}
25+
26+
}

0 commit comments

Comments
 (0)