Return to key-value service Key Value service
1: package com.voipplus.mmsclient.serviceLocalStorage;
2:
3: import android.app.Service;
4: import android.content.;
5: import android.os.Binder;
6: import android.os.IBinder;
7: import android.util.Log;
8:
9: import androidx.annotation.Nullable;
10:
11: import java.util.HashMap;
12: import java.util.Map;
13: import java.util.Set;
14:
15: public class KeyValueService extends Service {
16:
17: private static final String TAG = "KeyValueService";
18: private final Map<String, String> storage;
19:
20: public class LocalBinder extends Binder {
21: public KeyValueService getService() {
22: return KeyValueService.this;
23: }
24: }
25:
26: private final IBinder binder = new LocalBinder();
27:
28: public KeyValueService() {
29: storage = new HashMap<>();
30: }
31:
32: @Override
33: public void onCreate() {
34: super.onCreate();
35: Log.d(TAG, "KeyValueService created");
36: }
37:
38: @Nullable
39: @Override
40: public IBinder onBind( intent) {
41: Log.d(TAG, "KeyValueService bound");
42: return binder;
43: }
44:
45: @Override
46: public int onStartCommand( intent, int flags, int startId) {
47: Log.d(TAG, "KeyValueService started");
48: return START_STICKY;
49: }
50:
51: public void put(String key, String value) {
52: Log.d(TAG, "Putting key: " + key + ", value: " + value);
53: storage.put(key, value);
54: }
55:
56: public String get(String key) {
57: Log.d(TAG, "Getting key: " + key);
58: return storage.get(key);
59: }
60:
61: public void remove(String key) {
62: Log.d(TAG, "Removing key: " + key);
63: storage.remove(key);
64: }
65:
66: public boolean contains(String key) {
67: Log.d(TAG, "Checking if contains key: " + key);
68: return storage.containsKey(key);
69: }
70:
71: public Set<String> list() {
72: Log.d(TAG, "Listing keys");
73: return storage.keySet();
74: }
75: }
76:
Return to key-value service
AndroidMosaic context:
Comments (
)
)
Link to this page:
http://www.vb-net.com/AndroidMosaic/KeyValueService.htm
|
|