-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
android_tutorial
MMKV is an efficient, small, easy-to-use mobile key-value storage framework used in the WeChat application. It's currently available on both Android, iOS/macOS, Windows and POSIX.
You can use MMKV as you go. All changes are saved immediately, no sync
, no apply
calls needed.
-
Setup MMKV on App startup, say your
Application
class, add these code:public void onCreate() { super.onCreate(); String rootDir = MMKV.initialize(this); System.out.println("mmkv root: " + rootDir); }
-
MMKV has a global instance, you can use it directly:
import com.tencent.mmkv.MMKV; ... MMKV kv = MMKV.defaultMMKV(); kv.encode("bool", true); System.out.println("bool: " + kv.decodeBool("bool")); kv.encode("int", Integer.MIN_VALUE); System.out.println("int: " + kv.decodeInt("int")); kv.encode("long", Long.MAX_VALUE); System.out.println("long: " + kv.decodeLong("long")); kv.encode("float", -3.14f); System.out.println("float: " + kv.decodeFloat("float")); kv.encode("double", Double.MIN_VALUE); System.out.println("double: " + kv.decodeDouble("double")); kv.encode("string", "Hello from mmkv"); System.out.println("string: " + kv.decodeString("string")); byte[] bytes = {'m', 'm', 'k', 'v'}; kv.encode("bytes", bytes); System.out.println("bytes: " + new String(kv.decodeBytes("bytes")));
As you can see, MMKV is quite easy to use.
-
Deleting & Querying:
MMKV kv = MMKV.defaultMMKV(); kv.removeValueForKey("bool"); System.out.println("bool: " + kv.decodeBool("bool")); kv.removeValuesForKeys(new String[]{"int", "long"}); System.out.println("allKeys: " + Arrays.toString(kv.allKeys())); boolean hasBool = kv.containsKey("bool");
-
If different modules/logics need isolated storage, you can also create your own MMKV instance separately:
MMKV kv = MMKV.mmkvWithID("MyID"); kv.encode("bool", true);
-
If multi-process accessing is needed, you can set
MMKV.MULTI_PROCESS_MODE
on MMKV initialization:MMKV kv = MMKV.mmkvWithID("InterProcessKV", MMKV.MULTI_PROCESS_MODE); kv.encode("bool", true);
-
Primitive Types:
boolean, int, long, float, double, byte[]
-
Classes & Collections:
String, Set<String>
- Any class that implements
Parcelable
-
MMKV provides
importFromSharedPreferences()
, you can migrate from SharedPreferences with one line of code; -
MMKV implements
SharedPreferences
&SharedPreferences.Editor
. You can just change initialization & declaration to MMKV, and leave those CRUD codes untouched.private void testImportSharedPreferences() { //SharedPreferences preferences = getSharedPreferences("myData", MODE_PRIVATE); MMKV preferences = MMKV.mmkvWithID("myData"); // migration { SharedPreferences old_man = getSharedPreferences("myData", MODE_PRIVATE); preferences.importFromSharedPreferences(old_man); old_man.edit().clear().commit(); } // just use it as before SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean("bool", true); editor.putInt("int", Integer.MIN_VALUE); editor.putLong("long", Long.MAX_VALUE); editor.putFloat("float", -3.14f); editor.putString("string", "hello, imported"); HashSet<String> set = new HashSet<String>(); set.add("W"); set.add("e"); set.add("C"); set.add("h"); set.add("a"); set.add("t"); editor.putStringSet("string-set", set); // commit() is not needed any more //editor.commit(); }
MMKV is published under the BSD 3-Clause license. For details check out the LICENSE.TXT.
Check out the CHANGELOG.md for details of change history.
If you are interested in contributing, check out the CONTRIBUTING.md, also join our Tencent OpenSource Plan.
To give clarity of what is expected of our members, MMKV has adopted the code of conduct defined by the Contributor Covenant, which is widely used. And we think it articulates our values well. For more, check out the Code of Conduct.
Check out the FAQ first. Should there be any questions, don't hesitate to create issues.
User privacy is taken very seriously: MMKV does not obtain, collect or upload any personal information. Please refer to the MMKV SDK Personal Information Protection Rules for details.
- In English
- 中文
- In English
- 中文
- In English
- 中文
-
In English
-
中文
-
Golang