Package com.tencent.mmkv
Class MMKV
- java.lang.Object
-
- com.tencent.mmkv.MMKV
-
- All Implemented Interfaces:
android.content.SharedPreferences,android.content.SharedPreferences.Editor
public class MMKV extends java.lang.Object implements android.content.SharedPreferences, android.content.SharedPreferences.EditorAn highly efficient, reliable, multi-process key-value storage framework. THE PERFECT drop-in replacement for SharedPreferences and MultiProcessSharedPreferences.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceMMKV.LibLoaderThe interface for providing a 3rd library loader (the ReLinker https://github.com/KeepSafe/ReLinker, etc).
-
Field Summary
Fields Modifier and Type Field Description static intMULTI_PROCESS_MODEMulti-process mode.static intSINGLE_PROCESS_MODESingle-process mode.
-
Method Summary
Modifier and Type Method Description longactualSize()Get the actual used size of the MMKV instance.java.lang.String[]allKeys()voidapply()Deprecated.This method is only for compatibility purpose.intashmemFD()intashmemMetaFD()voidasync()Save all mmap memory to file asynchronously.static MMKVbackedUpMMKVWithID(java.lang.String mmapID, int mode, java.lang.String cryptKey, java.lang.String rootPath)Get an backed-up MMKV instance with customize settings all in one.static longbackupAllToDirectory(java.lang.String dstDir)backup all MMKV instance to dstDirstatic booleanbackupOneToDirectory(java.lang.String mmapID, java.lang.String dstDir, java.lang.String rootPath)backup one MMKV instance to dstDirvoidcheckContentChangedByOuterProcess()Check inter-process content change manually.voidcheckReSetCryptKey(java.lang.String cryptKey)Just reset the encryption key (will not encrypt or decrypt anything).android.content.SharedPreferences.Editorclear()voidclearAll()Clear all the key-values inside the MMKV instance.voidclearMemoryCache()Clear memory cache of the MMKV instance.voidclose()Call this method if the MMKV instance is no longer needed in the near future.booleancommit()Deprecated.This method is only for compatibility purpose.booleancontains(java.lang.String key)booleancontainsKey(java.lang.String key)Check whether or not MMKV contains the key.longcount()static NativeBuffercreateNativeBuffer(int size)Create an native buffer, whose underlying memory can be directly transferred to another JNI method.java.lang.StringcryptKey()booleandecodeBool(java.lang.String key)booleandecodeBool(java.lang.String key, boolean defaultValue)byte[]decodeBytes(java.lang.String key)byte[]decodeBytes(java.lang.String key, byte[] defaultValue)doubledecodeDouble(java.lang.String key)doubledecodeDouble(java.lang.String key, double defaultValue)floatdecodeFloat(java.lang.String key)floatdecodeFloat(java.lang.String key, float defaultValue)intdecodeInt(java.lang.String key)intdecodeInt(java.lang.String key, int defaultValue)longdecodeLong(java.lang.String key)longdecodeLong(java.lang.String key, long defaultValue)<T extends android.os.Parcelable>
TdecodeParcelable(java.lang.String key, java.lang.Class<T> tClass)<T extends android.os.Parcelable>
TdecodeParcelable(java.lang.String key, java.lang.Class<T> tClass, T defaultValue)java.lang.StringdecodeString(java.lang.String key)java.lang.StringdecodeString(java.lang.String key, java.lang.String defaultValue)java.util.Set<java.lang.String>decodeStringSet(java.lang.String key)java.util.Set<java.lang.String>decodeStringSet(java.lang.String key, java.util.Set<java.lang.String> defaultValue)java.util.Set<java.lang.String>decodeStringSet(java.lang.String key, java.util.Set<java.lang.String> defaultValue, java.lang.Class<? extends java.util.Set> cls)static MMKVdefaultMMKV()Create the default MMKV instance in single-process mode.static MMKVdefaultMMKV(int mode, java.lang.String cryptKey)Create the default MMKV instance in customize process mode, with an encryption key.static voiddestroyNativeBuffer(NativeBuffer buffer)Destroy the native buffer.static voiddisableProcessModeChecker()Manually disable the process mode checker.android.content.SharedPreferences.Editoredit()static voidenableProcessModeChecker()Manually enable the process mode checker.booleanencode(java.lang.String key, boolean value)booleanencode(java.lang.String key, byte[] value)booleanencode(java.lang.String key, double value)booleanencode(java.lang.String key, float value)booleanencode(java.lang.String key, int value)booleanencode(java.lang.String key, long value)booleanencode(java.lang.String key, android.os.Parcelable value)booleanencode(java.lang.String key, java.lang.String value)booleanencode(java.lang.String key, java.util.Set<java.lang.String> value)java.util.Map<java.lang.String,?>getAll()Intentionally Not Supported.booleangetBoolean(java.lang.String key, boolean defValue)byte[]getBytes(java.lang.String key, byte[] defValue)floatgetFloat(java.lang.String key, float defValue)intgetInt(java.lang.String key, int defValue)longgetLong(java.lang.String key, long defValue)static java.lang.StringgetRootDir()java.lang.StringgetString(java.lang.String key, java.lang.String defValue)java.util.Set<java.lang.String>getStringSet(java.lang.String key, java.util.Set<java.lang.String> defValues)intgetValueActualSize(java.lang.String key)Get the actual size of the key's value.intgetValueSize(java.lang.String key)Get the actual size consumption of the key's value.intimportFromSharedPreferences(android.content.SharedPreferences preferences)Atomically migrate all key-values from an existent SharedPreferences to the MMKV instance.static java.lang.Stringinitialize(android.content.Context context)Initialize MMKV with default configuration.static java.lang.Stringinitialize(android.content.Context context, MMKV.LibLoader loader)Initialize MMKV with a 3rd library loader.static java.lang.Stringinitialize(android.content.Context context, MMKV.LibLoader loader, MMKVLogLevel logLevel)Initialize MMKV with a 3rd library loader, and customize log level.static java.lang.Stringinitialize(android.content.Context context, MMKVLogLevel logLevel)Initialize MMKV with customize log level.static java.lang.Stringinitialize(android.content.Context context, java.lang.String rootDir)Initialize MMKV with customize root folder.static java.lang.Stringinitialize(android.content.Context context, java.lang.String rootDir, MMKV.LibLoader loader)Initialize MMKV with customize root folder, and a 3rd library loader.static java.lang.Stringinitialize(android.content.Context context, java.lang.String rootDir, MMKV.LibLoader loader, MMKVLogLevel logLevel)Initialize MMKV with customize settings.static java.lang.Stringinitialize(android.content.Context context, java.lang.String rootDir, MMKVLogLevel logLevel)Initialize MMKV with customize root folder, and log level.static java.lang.Stringinitialize(java.lang.String rootDir)Deprecated.This method is deprecated due to failing to automatically disable checkProcessMode() without Context.static java.lang.Stringinitialize(java.lang.String rootDir, MMKV.LibLoader loader)Deprecated.This method is deprecated due to failing to automatically disable checkProcessMode() without Context.static java.lang.Stringinitialize(java.lang.String rootDir, MMKV.LibLoader loader, MMKVLogLevel logLevel)Deprecated.This method is deprecated due to failing to automatically disable checkProcessMode() without Context.static java.lang.Stringinitialize(java.lang.String rootDir, MMKVLogLevel logLevel)Deprecated.This method is deprecated due to failing to automatically disable checkProcessMode() without Context.static booleanisFileValid(java.lang.String mmapID)Check whether the MMKV file is valid or not.static booleanisFileValid(java.lang.String mmapID, java.lang.String rootPath)Check whether the MMKV file is valid or not on customize folder.voidlock()Exclusively inter-process lock the MMKV instance.java.lang.StringmmapID()static MMKVmmkvWithAshmemFD(java.lang.String mmapID, int fd, int metaFD, java.lang.String cryptKey)Get an ashmem MMKV instance that has been initiated by another process.static MMKVmmkvWithAshmemID(android.content.Context context, java.lang.String mmapID, int size, int mode, java.lang.String cryptKey)Create an MMKV instance base on Anonymous Shared Memory, aka not synced to any disk files.static MMKVmmkvWithID(java.lang.String mmapID)Create an MMKV instance with an unique ID (in single-process mode).static MMKVmmkvWithID(java.lang.String mmapID, int mode)Create an MMKV instance in single-process or multi-process mode.static MMKVmmkvWithID(java.lang.String mmapID, int mode, java.lang.String cryptKey)Create an MMKV instance in customize process mode, with an encryption key.static MMKVmmkvWithID(java.lang.String mmapID, int mode, java.lang.String cryptKey, java.lang.String rootPath)Create an MMKV instance with customize settings all in one.static MMKVmmkvWithID(java.lang.String mmapID, java.lang.String rootPath)Create an MMKV instance in customize folder.static voidonExit()Notify MMKV that App is about to exit.static intpageSize()android.content.SharedPreferences.EditorputBoolean(java.lang.String key, boolean value)android.content.SharedPreferences.EditorputBytes(java.lang.String key, byte[] bytes)android.content.SharedPreferences.EditorputFloat(java.lang.String key, float value)android.content.SharedPreferences.EditorputInt(java.lang.String key, int value)android.content.SharedPreferences.EditorputLong(java.lang.String key, long value)android.content.SharedPreferences.EditorputString(java.lang.String key, java.lang.String value)android.content.SharedPreferences.EditorputStringSet(java.lang.String key, java.util.Set<java.lang.String> values)static voidregisterContentChangeNotify(MMKVContentChangeNotification notify)Register for MMKV inter-process content change notification.static voidregisterHandler(MMKVHandler handler)Register a handler for MMKV log redirecting, and error handling.voidregisterOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener listener)Intentionally Not Supported by MMKV.booleanreKey(java.lang.String cryptKey)Transform plain text into encrypted text, or vice versa by passing a null encryption key.android.content.SharedPreferences.Editorremove(java.lang.String key)voidremoveValueForKey(java.lang.String key)voidremoveValuesForKeys(java.lang.String[] arrKeys)Batch remove some keys from the MMKV instance.static longrestoreAllFromDirectory(java.lang.String srcDir)restore all MMKV instance from srcDirstatic booleanrestoreOneMMKVFromDirectory(java.lang.String mmapID, java.lang.String srcDir, java.lang.String rootPath)restore one MMKV instance from srcDirstatic voidsetLogLevel(MMKVLogLevel level)Set the log level of MMKV.voidsync()Save all mmap memory to file synchronously.longtotalSize()Get the size of the underlying file.voidtrim()ThetotalSize()of an MMKV instance won't reduce after deleting key-values, call this method after lots of deleting if you care about disk usage.booleantryLock()Try exclusively inter-process lock the MMKV instance.voidunlock()Exclusively inter-process unlock the MMKV instance.static voidunregisterContentChangeNotify()Unregister for MMKV inter-process content change notification.static voidunregisterHandler()Unregister the handler for MMKV.voidunregisterOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener listener)Intentionally Not Supported by MMKV.static java.lang.Stringversion()intwriteValueToNativeBuffer(java.lang.String key, NativeBuffer buffer)Write the value of the key to the native buffer.
-
-
-
Field Detail
-
SINGLE_PROCESS_MODE
public static final int SINGLE_PROCESS_MODE
Single-process mode. The default mode on an MMKV instance.- See Also:
- Constant Field Values
-
MULTI_PROCESS_MODE
public static final int MULTI_PROCESS_MODE
Multi-process mode. To enable multi-process accessing of an MMKV instance, you must set this mode whenever you getting that instance.- See Also:
- Constant Field Values
-
-
Method Detail
-
initialize
public static java.lang.String initialize(android.content.Context context)
Initialize MMKV with default configuration. You must call one of the initialize() methods on App startup process before using MMKV.- Parameters:
context- The context of Android App, usually from Application.- Returns:
- The root folder of MMKV, defaults to $(FilesDir)/mmkv.
-
initialize
public static java.lang.String initialize(android.content.Context context, MMKVLogLevel logLevel)Initialize MMKV with customize log level. You must call one of the initialize() methods on App startup process before using MMKV.- Parameters:
context- The context of Android App, usually from Application.logLevel- The log level of MMKV, defaults toMMKVLogLevel.LevelInfo.- Returns:
- The root folder of MMKV, defaults to $(FilesDir)/mmkv.
-
initialize
public static java.lang.String initialize(android.content.Context context, MMKV.LibLoader loader)Initialize MMKV with a 3rd library loader. You must call one of the initialize() methods on App startup process before using MMKV.- Parameters:
context- The context of Android App, usually from Application.loader- The 3rd library loader (for example, the ReLinker .- Returns:
- The root folder of MMKV, defaults to $(FilesDir)/mmkv.
-
initialize
public static java.lang.String initialize(android.content.Context context, MMKV.LibLoader loader, MMKVLogLevel logLevel)Initialize MMKV with a 3rd library loader, and customize log level. You must call one of the initialize() methods on App startup process before using MMKV.- Parameters:
context- The context of Android App, usually from Application.loader- The 3rd library loader (for example, the ReLinker .logLevel- The log level of MMKV, defaults toMMKVLogLevel.LevelInfo.- Returns:
- The root folder of MMKV, defaults to $(FilesDir)/mmkv.
-
initialize
public static java.lang.String initialize(android.content.Context context, java.lang.String rootDir)Initialize MMKV with customize root folder. You must call one of the initialize() methods on App startup process before using MMKV.- Parameters:
context- The context of Android App, usually from Application.rootDir- The root folder of MMKV, defaults to $(FilesDir)/mmkv.- Returns:
- The root folder of MMKV.
-
initialize
public static java.lang.String initialize(android.content.Context context, java.lang.String rootDir, MMKVLogLevel logLevel)Initialize MMKV with customize root folder, and log level. You must call one of the initialize() methods on App startup process before using MMKV.- Parameters:
context- The context of Android App, usually from Application.rootDir- The root folder of MMKV, defaults to $(FilesDir)/mmkv.logLevel- The log level of MMKV, defaults toMMKVLogLevel.LevelInfo.- Returns:
- The root folder of MMKV.
-
initialize
public static java.lang.String initialize(android.content.Context context, java.lang.String rootDir, MMKV.LibLoader loader)Initialize MMKV with customize root folder, and a 3rd library loader. You must call one of the initialize() methods on App startup process before using MMKV.- Parameters:
context- The context of Android App, usually from Application.rootDir- The root folder of MMKV, defaults to $(FilesDir)/mmkv.loader- The 3rd library loader (for example, the ReLinker .- Returns:
- The root folder of MMKV.
-
initialize
public static java.lang.String initialize(android.content.Context context, java.lang.String rootDir, MMKV.LibLoader loader, MMKVLogLevel logLevel)Initialize MMKV with customize settings. You must call one of the initialize() methods on App startup process before using MMKV.- Parameters:
context- The context of Android App, usually from Application.rootDir- The root folder of MMKV, defaults to $(FilesDir)/mmkv.loader- The 3rd library loader (for example, the ReLinker .logLevel- The log level of MMKV, defaults toMMKVLogLevel.LevelInfo.- Returns:
- The root folder of MMKV.
-
initialize
@Deprecated public static java.lang.String initialize(java.lang.String rootDir)
Deprecated.This method is deprecated due to failing to automatically disable checkProcessMode() without Context. Use theinitialize(Context, String)method instead.
-
initialize
@Deprecated public static java.lang.String initialize(java.lang.String rootDir, MMKVLogLevel logLevel)Deprecated.This method is deprecated due to failing to automatically disable checkProcessMode() without Context. Use theinitialize(Context, String, MMKVLogLevel)method instead.
-
initialize
@Deprecated public static java.lang.String initialize(java.lang.String rootDir, MMKV.LibLoader loader)Deprecated.This method is deprecated due to failing to automatically disable checkProcessMode() without Context. Use theinitialize(Context, String, LibLoader)method instead.
-
initialize
@Deprecated public static java.lang.String initialize(java.lang.String rootDir, MMKV.LibLoader loader, MMKVLogLevel logLevel)Deprecated.This method is deprecated due to failing to automatically disable checkProcessMode() without Context. Use theinitialize(Context, String, LibLoader, MMKVLogLevel)method instead.
-
getRootDir
public static java.lang.String getRootDir()
- Returns:
- The root folder of MMKV, defaults to $(FilesDir)/mmkv.
-
setLogLevel
public static void setLogLevel(MMKVLogLevel level)
Set the log level of MMKV.- Parameters:
level- Defaults toMMKVLogLevel.LevelInfo.
-
onExit
public static void onExit()
Notify MMKV that App is about to exit. It's totally fine not calling it at all.
-
mmkvWithID
public static MMKV mmkvWithID(java.lang.String mmapID) throws java.lang.RuntimeException
Create an MMKV instance with an unique ID (in single-process mode).- Parameters:
mmapID- The unique ID of the MMKV instance.- Throws:
java.lang.RuntimeException- if there's an runtime error.
-
mmkvWithID
public static MMKV mmkvWithID(java.lang.String mmapID, int mode) throws java.lang.RuntimeException
Create an MMKV instance in single-process or multi-process mode.- Parameters:
mmapID- The unique ID of the MMKV instance.mode- The process mode of the MMKV instance, defaults toSINGLE_PROCESS_MODE.- Throws:
java.lang.RuntimeException- if there's an runtime error.
-
mmkvWithID
public static MMKV mmkvWithID(java.lang.String mmapID, int mode, @Nullable java.lang.String cryptKey) throws java.lang.RuntimeException
Create an MMKV instance in customize process mode, with an encryption key.- Parameters:
mmapID- The unique ID of the MMKV instance.mode- The process mode of the MMKV instance, defaults toSINGLE_PROCESS_MODE.cryptKey- The encryption key of the MMKV instance (no more than 16 bytes).- Throws:
java.lang.RuntimeException- if there's an runtime error.
-
mmkvWithID
public static MMKV mmkvWithID(java.lang.String mmapID, java.lang.String rootPath) throws java.lang.RuntimeException
Create an MMKV instance in customize folder.- Parameters:
mmapID- The unique ID of the MMKV instance.rootPath- The folder of the MMKV instance, defaults to $(FilesDir)/mmkv.- Throws:
java.lang.RuntimeException- if there's an runtime error.
-
mmkvWithID
public static MMKV mmkvWithID(java.lang.String mmapID, int mode, @Nullable java.lang.String cryptKey, java.lang.String rootPath) throws java.lang.RuntimeException
Create an MMKV instance with customize settings all in one.- Parameters:
mmapID- The unique ID of the MMKV instance.mode- The process mode of the MMKV instance, defaults toSINGLE_PROCESS_MODE.cryptKey- The encryption key of the MMKV instance (no more than 16 bytes).rootPath- The folder of the MMKV instance, defaults to $(FilesDir)/mmkv.- Throws:
java.lang.RuntimeException- if there's an runtime error.
-
backedUpMMKVWithID
public static MMKV backedUpMMKVWithID(java.lang.String mmapID, int mode, @Nullable java.lang.String cryptKey, java.lang.String rootPath) throws java.lang.RuntimeException
Get an backed-up MMKV instance with customize settings all in one.- Parameters:
mmapID- The unique ID of the MMKV instance.mode- The process mode of the MMKV instance, defaults toSINGLE_PROCESS_MODE.cryptKey- The encryption key of the MMKV instance (no more than 16 bytes).rootPath- The backup folder of the MMKV instance.- Throws:
java.lang.RuntimeException- if there's an runtime error.
-
mmkvWithAshmemID
public static MMKV mmkvWithAshmemID(android.content.Context context, java.lang.String mmapID, int size, int mode, @Nullable java.lang.String cryptKey) throws java.lang.RuntimeException
Create an MMKV instance base on Anonymous Shared Memory, aka not synced to any disk files.- Parameters:
context- The context of Android App, usually from Application.mmapID- The unique ID of the MMKV instance.size- The maximum size of the underlying Anonymous Shared Memory. Anonymous Shared Memory on Android can't grow dynamically, must set an appropriate size on creation.mode- The process mode of the MMKV instance, defaults toSINGLE_PROCESS_MODE.cryptKey- The encryption key of the MMKV instance (no more than 16 bytes).- Throws:
java.lang.RuntimeException- if there's an runtime error.
-
defaultMMKV
public static MMKV defaultMMKV() throws java.lang.RuntimeException
Create the default MMKV instance in single-process mode.- Throws:
java.lang.RuntimeException- if there's an runtime error.
-
defaultMMKV
public static MMKV defaultMMKV(int mode, @Nullable java.lang.String cryptKey) throws java.lang.RuntimeException
Create the default MMKV instance in customize process mode, with an encryption key.- Parameters:
mode- The process mode of the MMKV instance, defaults toSINGLE_PROCESS_MODE.cryptKey- The encryption key of the MMKV instance (no more than 16 bytes).- Throws:
java.lang.RuntimeException- if there's an runtime error.
-
enableProcessModeChecker
public static void enableProcessModeChecker()
Manually enable the process mode checker. By default, it's automatically enabled in DEBUG build, and disabled in RELEASE build. If it's enabled, MMKV will throw exceptions when an MMKV instance is created with mismatch process mode.
-
disableProcessModeChecker
public static void disableProcessModeChecker()
Manually disable the process mode checker. By default, it's automatically enabled in DEBUG build, and disabled in RELEASE build. If it's enabled, MMKV will throw exceptions when an MMKV instance is created with mismatch process mode.
-
cryptKey
@Nullable public java.lang.String cryptKey()
- Returns:
- The encryption key (no more than 16 bytes).
-
reKey
public boolean reKey(@Nullable java.lang.String cryptKey)Transform plain text into encrypted text, or vice versa by passing a null encryption key. You can also change existing crypt key with a different cryptKey.- Parameters:
cryptKey- The new encryption key (no more than 16 bytes).- Returns:
- True if success, otherwise False.
-
checkReSetCryptKey
public void checkReSetCryptKey(@Nullable java.lang.String cryptKey)Just reset the encryption key (will not encrypt or decrypt anything). Usually you should call this method after another process hasreKey(String)the multi-process MMKV instance.- Parameters:
cryptKey- The new encryption key (no more than 16 bytes).
-
pageSize
public static int pageSize()
- Returns:
- The device's memory page size.
-
version
public static java.lang.String version()
- Returns:
- The version of MMKV.
-
mmapID
public java.lang.String mmapID()
- Returns:
- The unique ID of the MMKV instance.
-
lock
public void lock()
Exclusively inter-process lock the MMKV instance. It will block and wait until it successfully locks the file. It will make no effect if the MMKV instance is created withSINGLE_PROCESS_MODE.
-
unlock
public void unlock()
Exclusively inter-process unlock the MMKV instance. It will make no effect if the MMKV instance is created withSINGLE_PROCESS_MODE.
-
tryLock
public boolean tryLock()
Try exclusively inter-process lock the MMKV instance. It will not block if the file has already been locked by another process. It will make no effect if the MMKV instance is created withSINGLE_PROCESS_MODE.- Returns:
- True if successfully locked, otherwise return immediately with False.
-
encode
public boolean encode(java.lang.String key, boolean value)
-
decodeBool
public boolean decodeBool(java.lang.String key)
-
decodeBool
public boolean decodeBool(java.lang.String key, boolean defaultValue)
-
encode
public boolean encode(java.lang.String key, int value)
-
decodeInt
public int decodeInt(java.lang.String key)
-
decodeInt
public int decodeInt(java.lang.String key, int defaultValue)
-
encode
public boolean encode(java.lang.String key, long value)
-
decodeLong
public long decodeLong(java.lang.String key)
-
decodeLong
public long decodeLong(java.lang.String key, long defaultValue)
-
encode
public boolean encode(java.lang.String key, float value)
-
decodeFloat
public float decodeFloat(java.lang.String key)
-
decodeFloat
public float decodeFloat(java.lang.String key, float defaultValue)
-
encode
public boolean encode(java.lang.String key, double value)
-
decodeDouble
public double decodeDouble(java.lang.String key)
-
decodeDouble
public double decodeDouble(java.lang.String key, double defaultValue)
-
encode
public boolean encode(java.lang.String key, @Nullable java.lang.String value)
-
decodeString
@Nullable public java.lang.String decodeString(java.lang.String key)
-
decodeString
@Nullable public java.lang.String decodeString(java.lang.String key, @Nullable java.lang.String defaultValue)
-
encode
public boolean encode(java.lang.String key, @Nullable java.util.Set<java.lang.String> value)
-
decodeStringSet
@Nullable public java.util.Set<java.lang.String> decodeStringSet(java.lang.String key)
-
decodeStringSet
@Nullable public java.util.Set<java.lang.String> decodeStringSet(java.lang.String key, @Nullable java.util.Set<java.lang.String> defaultValue)
-
decodeStringSet
@Nullable public java.util.Set<java.lang.String> decodeStringSet(java.lang.String key, @Nullable java.util.Set<java.lang.String> defaultValue, java.lang.Class<? extends java.util.Set> cls)
-
encode
public boolean encode(java.lang.String key, @Nullable byte[] value)
-
decodeBytes
@Nullable public byte[] decodeBytes(java.lang.String key)
-
decodeBytes
@Nullable public byte[] decodeBytes(java.lang.String key, @Nullable byte[] defaultValue)
-
encode
public boolean encode(java.lang.String key, @Nullable android.os.Parcelable value)
-
decodeParcelable
@Nullable public <T extends android.os.Parcelable> T decodeParcelable(java.lang.String key, java.lang.Class<T> tClass)
-
decodeParcelable
@Nullable public <T extends android.os.Parcelable> T decodeParcelable(java.lang.String key, java.lang.Class<T> tClass, @Nullable T defaultValue)
-
getValueSize
public int getValueSize(java.lang.String key)
Get the actual size consumption of the key's value. Note: might be a little bigger than value's length.- Parameters:
key- The key of the value.
-
getValueActualSize
public int getValueActualSize(java.lang.String key)
Get the actual size of the key's value. String's length or byte[]'s length, etc.- Parameters:
key- The key of the value.
-
containsKey
public boolean containsKey(java.lang.String key)
Check whether or not MMKV contains the key.- Parameters:
key- The key of the value.
-
allKeys
@Nullable public java.lang.String[] allKeys()
- Returns:
- All the keys.
-
count
public long count()
- Returns:
- The total count of all the keys.
-
totalSize
public long totalSize()
Get the size of the underlying file. Align to the disk block size, typically 4K for an Android device.
-
actualSize
public long actualSize()
Get the actual used size of the MMKV instance. This size might increase and decrease as MMKV doing insertion and full write back.
-
removeValueForKey
public void removeValueForKey(java.lang.String key)
-
removeValuesForKeys
public void removeValuesForKeys(java.lang.String[] arrKeys)
Batch remove some keys from the MMKV instance.- Parameters:
arrKeys- The keys to be removed.
-
clearAll
public void clearAll()
Clear all the key-values inside the MMKV instance.
-
trim
public void trim()
ThetotalSize()of an MMKV instance won't reduce after deleting key-values, call this method after lots of deleting if you care about disk usage. Note thatclearAll()has a similar effect.
-
close
public void close()
Call this method if the MMKV instance is no longer needed in the near future. Any subsequent call to any MMKV instances with the same ID is undefined behavior.
-
clearMemoryCache
public void clearMemoryCache()
Clear memory cache of the MMKV instance. You can call it on memory warning. Any subsequent call to the MMKV instance will trigger all key-values loading from the file again.
-
sync
public void sync()
Save all mmap memory to file synchronously. You don't need to call this, really, I mean it. Unless you worry about the device running out of battery.
-
async
public void async()
Save all mmap memory to file asynchronously. No need to call this unless you worry about the device running out of battery.
-
isFileValid
public static boolean isFileValid(java.lang.String mmapID)
Check whether the MMKV file is valid or not. Note: Don't use this to check the existence of the instance, the result is undefined on nonexistent files.
-
isFileValid
public static boolean isFileValid(java.lang.String mmapID, @Nullable java.lang.String rootPath)Check whether the MMKV file is valid or not on customize folder.- Parameters:
mmapID- The unique ID of the MMKV instance.rootPath- The folder of the MMKV instance, defaults to $(FilesDir)/mmkv.
-
importFromSharedPreferences
public int importFromSharedPreferences(android.content.SharedPreferences preferences)
Atomically migrate all key-values from an existent SharedPreferences to the MMKV instance.- Parameters:
preferences- The SharedPreferences to import from.- Returns:
- The total count of key-values imported.
-
backupOneToDirectory
public static boolean backupOneToDirectory(java.lang.String mmapID, java.lang.String dstDir, @Nullable java.lang.String rootPath)backup one MMKV instance to dstDir- Parameters:
mmapID- the MMKV ID to backuprootPath- the customize root path of the MMKV, if null then backup from the root dir of MMKVdstDir- the backup destination directory
-
restoreOneMMKVFromDirectory
public static boolean restoreOneMMKVFromDirectory(java.lang.String mmapID, java.lang.String srcDir, @Nullable java.lang.String rootPath)restore one MMKV instance from srcDir- Parameters:
mmapID- the MMKV ID to restoresrcDir- the restore source directoryrootPath- the customize root path of the MMKV, if null then restore to the root dir of MMKV
-
backupAllToDirectory
public static long backupAllToDirectory(java.lang.String dstDir)
backup all MMKV instance to dstDir- Parameters:
dstDir- the backup destination directory- Returns:
- count of MMKV successfully backuped
-
restoreAllFromDirectory
public static long restoreAllFromDirectory(java.lang.String srcDir)
restore all MMKV instance from srcDir- Parameters:
srcDir- the restore source directory- Returns:
- count of MMKV successfully restored
-
getAll
public java.util.Map<java.lang.String,?> getAll()
Intentionally Not Supported. Because MMKV does type-eraser inside to get better performance.- Specified by:
getAllin interfaceandroid.content.SharedPreferences
-
getString
@Nullable public java.lang.String getString(java.lang.String key, @Nullable java.lang.String defValue)- Specified by:
getStringin interfaceandroid.content.SharedPreferences
-
putString
public android.content.SharedPreferences.Editor putString(java.lang.String key, @Nullable java.lang.String value)- Specified by:
putStringin interfaceandroid.content.SharedPreferences.Editor
-
getStringSet
@Nullable public java.util.Set<java.lang.String> getStringSet(java.lang.String key, @Nullable java.util.Set<java.lang.String> defValues)- Specified by:
getStringSetin interfaceandroid.content.SharedPreferences
-
putStringSet
public android.content.SharedPreferences.Editor putStringSet(java.lang.String key, @Nullable java.util.Set<java.lang.String> values)- Specified by:
putStringSetin interfaceandroid.content.SharedPreferences.Editor
-
putBytes
public android.content.SharedPreferences.Editor putBytes(java.lang.String key, @Nullable byte[] bytes)
-
getBytes
public byte[] getBytes(java.lang.String key, @Nullable byte[] defValue)
-
getInt
public int getInt(java.lang.String key, int defValue)- Specified by:
getIntin interfaceandroid.content.SharedPreferences
-
putInt
public android.content.SharedPreferences.Editor putInt(java.lang.String key, int value)- Specified by:
putIntin interfaceandroid.content.SharedPreferences.Editor
-
getLong
public long getLong(java.lang.String key, long defValue)- Specified by:
getLongin interfaceandroid.content.SharedPreferences
-
putLong
public android.content.SharedPreferences.Editor putLong(java.lang.String key, long value)- Specified by:
putLongin interfaceandroid.content.SharedPreferences.Editor
-
getFloat
public float getFloat(java.lang.String key, float defValue)- Specified by:
getFloatin interfaceandroid.content.SharedPreferences
-
putFloat
public android.content.SharedPreferences.Editor putFloat(java.lang.String key, float value)- Specified by:
putFloatin interfaceandroid.content.SharedPreferences.Editor
-
getBoolean
public boolean getBoolean(java.lang.String key, boolean defValue)- Specified by:
getBooleanin interfaceandroid.content.SharedPreferences
-
putBoolean
public android.content.SharedPreferences.Editor putBoolean(java.lang.String key, boolean value)- Specified by:
putBooleanin interfaceandroid.content.SharedPreferences.Editor
-
remove
public android.content.SharedPreferences.Editor remove(java.lang.String key)
- Specified by:
removein interfaceandroid.content.SharedPreferences.Editor
-
clear
public android.content.SharedPreferences.Editor clear()
- Specified by:
clearin interfaceandroid.content.SharedPreferences.Editor
-
commit
@Deprecated public boolean commit()
Deprecated.- Specified by:
commitin interfaceandroid.content.SharedPreferences.Editor
-
apply
@Deprecated public void apply()
Deprecated.This method is only for compatibility purpose. You should remove all the calls after migration to MMKV. MMKV doesn't rely on apply() to save data to file. If you really worry about losing battery and data corruption, callasync()instead.- Specified by:
applyin interfaceandroid.content.SharedPreferences.Editor
-
contains
public boolean contains(java.lang.String key)
- Specified by:
containsin interfaceandroid.content.SharedPreferences
-
edit
public android.content.SharedPreferences.Editor edit()
- Specified by:
editin interfaceandroid.content.SharedPreferences
-
registerOnSharedPreferenceChangeListener
public void registerOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener listener)
Intentionally Not Supported by MMKV. We believe it's better not for a storage framework to notify the change of data. CheckregisterContentChangeNotify(com.tencent.mmkv.MMKVContentChangeNotification)for a potential replacement on inter-process scene.- Specified by:
registerOnSharedPreferenceChangeListenerin interfaceandroid.content.SharedPreferences
-
unregisterOnSharedPreferenceChangeListener
public void unregisterOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener listener)
Intentionally Not Supported by MMKV. We believe it's better not for a storage framework to notify the change of data.- Specified by:
unregisterOnSharedPreferenceChangeListenerin interfaceandroid.content.SharedPreferences
-
mmkvWithAshmemFD
public static MMKV mmkvWithAshmemFD(java.lang.String mmapID, int fd, int metaFD, java.lang.String cryptKey) throws java.lang.RuntimeException
Get an ashmem MMKV instance that has been initiated by another process. Normally you should just callmmkvWithAshmemID(Context, String, int, int, String)instead.- Parameters:
mmapID- The unique ID of the MMKV instance.fd- The file descriptor of the ashmem of the MMKV file, transferred from another process by binder.metaFD- The file descriptor of the ashmem of the MMKV crc file, transferred from another process by binder.cryptKey- The encryption key of the MMKV instance (no more than 16 bytes).- Throws:
java.lang.RuntimeException- If any failure in JNI or runtime.
-
ashmemFD
public int ashmemFD()
- Returns:
- The file descriptor of the ashmem of the MMKV file.
-
ashmemMetaFD
public int ashmemMetaFD()
- Returns:
- The file descriptor of the ashmem of the MMKV crc file.
-
createNativeBuffer
@Nullable public static NativeBuffer createNativeBuffer(int size)
Create an native buffer, whose underlying memory can be directly transferred to another JNI method. Avoiding unnecessary JNI boxing and unboxing. An NativeBuffer must be manuallydestroyNativeBuffer(com.tencent.mmkv.NativeBuffer)to avoid memory leak.- Parameters:
size- The size of the underlying memory.
-
destroyNativeBuffer
public static void destroyNativeBuffer(NativeBuffer buffer)
Destroy the native buffer. An NativeBuffer must be manually destroy to avoid memory leak.
-
writeValueToNativeBuffer
public int writeValueToNativeBuffer(java.lang.String key, NativeBuffer buffer)Write the value of the key to the native buffer.- Returns:
- The size written. Return -1 on any error.
-
registerHandler
public static void registerHandler(MMKVHandler handler)
Register a handler for MMKV log redirecting, and error handling.
-
unregisterHandler
public static void unregisterHandler()
Unregister the handler for MMKV.
-
registerContentChangeNotify
public static void registerContentChangeNotify(MMKVContentChangeNotification notify)
Register for MMKV inter-process content change notification. The notification will trigger only when any method is manually called on the MMKV instance. For examplecheckContentChangedByOuterProcess().- Parameters:
notify- The notification handler.
-
unregisterContentChangeNotify
public static void unregisterContentChangeNotify()
Unregister for MMKV inter-process content change notification.
-
checkContentChangedByOuterProcess
public void checkContentChangedByOuterProcess()
Check inter-process content change manually.
-
-