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.Editor
An 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 interface
MMKV.LibLoader
The interface for providing a 3rd library loader (the ReLinker https://github.com/KeepSafe/ReLinker, etc).
-
Field Summary
Fields Modifier and Type Field Description static int
ExpireInDay
static int
ExpireInHour
static int
ExpireInMinute
static int
ExpireInMonth
static int
ExpireInYear
static int
ExpireNever
static int
MULTI_PROCESS_MODE
Multi-process mode.static int
SINGLE_PROCESS_MODE
Single-process mode.
-
Method Summary
Modifier and Type Method Description long
actualSize()
Get the actual used size of the MMKV instance.java.lang.String[]
allKeys()
java.lang.String[]
allNonExpireKeys()
void
apply()
Deprecated.This method is only for compatibility purpose.int
ashmemFD()
int
ashmemMetaFD()
void
async()
Save all mmap memory to file asynchronously.static MMKV
backedUpMMKVWithID(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 long
backupAllToDirectory(java.lang.String dstDir)
backup all MMKV instance to dstDirstatic boolean
backupOneToDirectory(java.lang.String mmapID, java.lang.String dstDir, java.lang.String rootPath)
backup one MMKV instance to dstDirvoid
checkContentChangedByOuterProcess()
Check inter-process content change manually.void
checkReSetCryptKey(java.lang.String cryptKey)
Just reset the encryption key (will not encrypt or decrypt anything).android.content.SharedPreferences.Editor
clear()
void
clearAll()
Clear all the key-values inside the MMKV instance.void
clearMemoryCache()
Clear memory cache of the MMKV instance.void
close()
Call this method if the MMKV instance is no longer needed in the near future.boolean
commit()
Deprecated.This method is only for compatibility purpose.boolean
contains(java.lang.String key)
boolean
containsKey(java.lang.String key)
Check whether or not MMKV contains the key.long
count()
long
countNonExpiredKeys()
static NativeBuffer
createNativeBuffer(int size)
Create an native buffer, whose underlying memory can be directly transferred to another JNI method.java.lang.String
cryptKey()
boolean
decodeBool(java.lang.String key)
boolean
decodeBool(java.lang.String key, boolean defaultValue)
byte[]
decodeBytes(java.lang.String key)
byte[]
decodeBytes(java.lang.String key, byte[] defaultValue)
double
decodeDouble(java.lang.String key)
double
decodeDouble(java.lang.String key, double defaultValue)
float
decodeFloat(java.lang.String key)
float
decodeFloat(java.lang.String key, float defaultValue)
int
decodeInt(java.lang.String key)
int
decodeInt(java.lang.String key, int defaultValue)
long
decodeLong(java.lang.String key)
long
decodeLong(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.String
decodeString(java.lang.String key)
java.lang.String
decodeString(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 MMKV
defaultMMKV()
Create the default MMKV instance in single-process mode.static MMKV
defaultMMKV(int mode, java.lang.String cryptKey)
Create the default MMKV instance in customize process mode, with an encryption key.static void
destroyNativeBuffer(NativeBuffer buffer)
Destroy the native buffer.boolean
disableAutoKeyExpire()
Disable auto key expiration.static void
disableProcessModeChecker()
Manually disable the process mode checker.android.content.SharedPreferences.Editor
edit()
boolean
enableAutoKeyExpire(int expireDurationInSecond)
Enable auto key expiration.static void
enableProcessModeChecker()
Manually enable the process mode checker.boolean
encode(java.lang.String key, boolean value)
boolean
encode(java.lang.String key, boolean value, int expireDurationInSecond)
Set value with customize expiration in sections.boolean
encode(java.lang.String key, byte[] value)
boolean
encode(java.lang.String key, byte[] value, int expireDurationInSecond)
Set value with customize expiration in sections.boolean
encode(java.lang.String key, double value)
boolean
encode(java.lang.String key, double value, int expireDurationInSecond)
Set value with customize expiration in sections.boolean
encode(java.lang.String key, float value)
boolean
encode(java.lang.String key, float value, int expireDurationInSecond)
Set value with customize expiration in sections.boolean
encode(java.lang.String key, int value)
boolean
encode(java.lang.String key, int value, int expireDurationInSecond)
Set value with customize expiration in sections.boolean
encode(java.lang.String key, long value)
boolean
encode(java.lang.String key, long value, int expireDurationInSecond)
Set value with customize expiration in sections.boolean
encode(java.lang.String key, android.os.Parcelable value)
boolean
encode(java.lang.String key, android.os.Parcelable value, int expireDurationInSecond)
Set value with customize expiration in sections.boolean
encode(java.lang.String key, java.lang.String value)
boolean
encode(java.lang.String key, java.lang.String value, int expireDurationInSecond)
Set value with customize expiration in sections.boolean
encode(java.lang.String key, java.util.Set<java.lang.String> value)
boolean
encode(java.lang.String key, java.util.Set<java.lang.String> value, int expireDurationInSecond)
Set value with customize expiration in sections.java.util.Map<java.lang.String,?>
getAll()
Intentionally Not Supported.boolean
getBoolean(java.lang.String key, boolean defValue)
byte[]
getBytes(java.lang.String key, byte[] defValue)
float
getFloat(java.lang.String key, float defValue)
int
getInt(java.lang.String key, int defValue)
long
getLong(java.lang.String key, long defValue)
static java.lang.String
getRootDir()
java.lang.String
getString(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)
int
getValueActualSize(java.lang.String key)
Get the actual size of the key's value.int
getValueSize(java.lang.String key)
Get the actual size consumption of the key's value.int
importFromSharedPreferences(android.content.SharedPreferences preferences)
Atomically migrate all key-values from an existent SharedPreferences to the MMKV instance.static java.lang.String
initialize(android.content.Context context)
Initialize MMKV with default configuration.static java.lang.String
initialize(android.content.Context context, MMKV.LibLoader loader)
Initialize MMKV with a 3rd library loader.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.static java.lang.String
initialize(android.content.Context context, MMKVLogLevel logLevel)
Initialize MMKV with customize log level.static java.lang.String
initialize(android.content.Context context, java.lang.String rootDir)
Initialize MMKV with customize root folder.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.static java.lang.String
initialize(android.content.Context context, java.lang.String rootDir, MMKV.LibLoader loader, MMKVLogLevel logLevel)
Initialize MMKV with customize settings.static java.lang.String
initialize(android.content.Context context, java.lang.String rootDir, MMKV.LibLoader loader, MMKVLogLevel logLevel, MMKVHandler handler)
static java.lang.String
initialize(android.content.Context context, java.lang.String rootDir, MMKVLogLevel logLevel)
Initialize MMKV with customize root folder, and log level.static java.lang.String
initialize(java.lang.String rootDir)
Deprecated.This method is deprecated due to failing to automatically disable checkProcessMode() without Context.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.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.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.static boolean
isFileValid(java.lang.String mmapID)
Check whether the MMKV file is valid or not.static boolean
isFileValid(java.lang.String mmapID, java.lang.String rootPath)
Check whether the MMKV file is valid or not on customize folder.void
lock()
Exclusively inter-process lock the MMKV instance.java.lang.String
mmapID()
static MMKV
mmkvWithAshmemFD(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 MMKV
mmkvWithAshmemID(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 MMKV
mmkvWithID(java.lang.String mmapID)
Create an MMKV instance with an unique ID (in single-process mode).static MMKV
mmkvWithID(java.lang.String mmapID, int mode)
Create an MMKV instance in single-process or multi-process mode.static MMKV
mmkvWithID(java.lang.String mmapID, int mode, java.lang.String cryptKey)
Create an MMKV instance in customize process mode, with an encryption key.static MMKV
mmkvWithID(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 MMKV
mmkvWithID(java.lang.String mmapID, java.lang.String rootPath)
Create an MMKV instance in customize folder.static void
onExit()
Notify MMKV that App is about to exit.static int
pageSize()
android.content.SharedPreferences.Editor
putBoolean(java.lang.String key, boolean value)
android.content.SharedPreferences.Editor
putBoolean(java.lang.String key, boolean value, int expireDurationInSecond)
android.content.SharedPreferences.Editor
putBytes(java.lang.String key, byte[] bytes)
android.content.SharedPreferences.Editor
putBytes(java.lang.String key, byte[] bytes, int expireDurationInSecond)
android.content.SharedPreferences.Editor
putFloat(java.lang.String key, float value)
android.content.SharedPreferences.Editor
putFloat(java.lang.String key, float value, int expireDurationInSecond)
android.content.SharedPreferences.Editor
putInt(java.lang.String key, int value)
android.content.SharedPreferences.Editor
putInt(java.lang.String key, int value, int expireDurationInSecond)
android.content.SharedPreferences.Editor
putLong(java.lang.String key, long value)
android.content.SharedPreferences.Editor
putLong(java.lang.String key, long value, int expireDurationInSecond)
android.content.SharedPreferences.Editor
putString(java.lang.String key, java.lang.String value)
android.content.SharedPreferences.Editor
putString(java.lang.String key, java.lang.String value, int expireDurationInSecond)
android.content.SharedPreferences.Editor
putStringSet(java.lang.String key, java.util.Set<java.lang.String> values)
android.content.SharedPreferences.Editor
putStringSet(java.lang.String key, java.util.Set<java.lang.String> values, int expireDurationInSecond)
static void
registerContentChangeNotify(MMKVContentChangeNotification notify)
Register for MMKV inter-process content change notification.static void
registerHandler(MMKVHandler handler)
Deprecated.This method is deprecated.void
registerOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener listener)
Intentionally Not Supported by MMKV.boolean
reKey(java.lang.String cryptKey)
Transform plain text into encrypted text, or vice versa by passing a null encryption key.android.content.SharedPreferences.Editor
remove(java.lang.String key)
void
removeValueForKey(java.lang.String key)
void
removeValuesForKeys(java.lang.String[] arrKeys)
Batch remove some keys from the MMKV instance.static long
restoreAllFromDirectory(java.lang.String srcDir)
restore all MMKV instance from srcDirstatic boolean
restoreOneMMKVFromDirectory(java.lang.String mmapID, java.lang.String srcDir, java.lang.String rootPath)
restore one MMKV instance from srcDirstatic void
setLogLevel(MMKVLogLevel level)
Set the log level of MMKV.void
sync()
Save all mmap memory to file synchronously.long
totalSize()
Get the size of the underlying file.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.boolean
tryLock()
Try exclusively inter-process lock the MMKV instance.void
unlock()
Exclusively inter-process unlock the MMKV instance.static void
unregisterContentChangeNotify()
Unregister for MMKV inter-process content change notification.static void
unregisterHandler()
Unregister the handler for MMKV.void
unregisterOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener listener)
Intentionally Not Supported by MMKV.static java.lang.String
version()
int
writeValueToNativeBuffer(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
-
ExpireNever
public static final int ExpireNever
- See Also:
- Constant Field Values
-
ExpireInMinute
public static final int ExpireInMinute
- See Also:
- Constant Field Values
-
ExpireInHour
public static final int ExpireInHour
- See Also:
- Constant Field Values
-
ExpireInDay
public static final int ExpireInDay
- See Also:
- Constant Field Values
-
ExpireInMonth
public static final int ExpireInMonth
- See Also:
- Constant Field Values
-
ExpireInYear
public static final int ExpireInYear
- 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
public static java.lang.String initialize(android.content.Context context, java.lang.String rootDir, MMKV.LibLoader loader, MMKVLogLevel logLevel, MMKVHandler handler)
-
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)
-
encode
public boolean encode(java.lang.String key, boolean value, int expireDurationInSecond)
Set value with customize expiration in sections.- Parameters:
expireDurationInSecond
- override the default duration,ExpireNever
(0) means never expire.
-
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)
-
encode
public boolean encode(java.lang.String key, int value, int expireDurationInSecond)
Set value with customize expiration in sections.- Parameters:
expireDurationInSecond
- override the default duration,ExpireNever
(0) means never expire.
-
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)
-
encode
public boolean encode(java.lang.String key, long value, int expireDurationInSecond)
Set value with customize expiration in sections.- Parameters:
expireDurationInSecond
- override the default duration,ExpireNever
(0) means never expire.
-
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)
-
encode
public boolean encode(java.lang.String key, float value, int expireDurationInSecond)
Set value with customize expiration in sections.- Parameters:
expireDurationInSecond
- override the default duration,ExpireNever
(0) means never expire.
-
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)
-
encode
public boolean encode(java.lang.String key, double value, int expireDurationInSecond)
Set value with customize expiration in sections.- Parameters:
expireDurationInSecond
- override the default duration,ExpireNever
(0) means never expire.
-
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)
-
encode
public boolean encode(java.lang.String key, @Nullable java.lang.String value, int expireDurationInSecond)
Set value with customize expiration in sections.- Parameters:
expireDurationInSecond
- override the default duration,ExpireNever
(0) means never expire.
-
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)
-
encode
public boolean encode(java.lang.String key, @Nullable java.util.Set<java.lang.String> value, int expireDurationInSecond)
Set value with customize expiration in sections.- Parameters:
expireDurationInSecond
- override the default duration,ExpireNever
(0) means never expire.
-
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)
-
encode
public boolean encode(java.lang.String key, @Nullable byte[] value, int expireDurationInSecond)
Set value with customize expiration in sections.- Parameters:
expireDurationInSecond
- override the default duration,ExpireNever
(0) means never expire.
-
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)
-
encode
public boolean encode(java.lang.String key, @Nullable android.os.Parcelable value, int expireDurationInSecond)
Set value with customize expiration in sections.- Parameters:
expireDurationInSecond
- override the default duration,ExpireNever
(0) means never expire.
-
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.
-
allNonExpireKeys
@Nullable public java.lang.String[] allNonExpireKeys()
- Returns:
- All non-expired keys. Note that this call has costs.
-
count
public long count()
- Returns:
- The total count of all the keys.
-
countNonExpiredKeys
public long countNonExpiredKeys()
- Returns:
- The total count of all non-expired keys. Note that this call has costs.
-
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
-
enableAutoKeyExpire
public boolean enableAutoKeyExpire(int expireDurationInSecond)
Enable auto key expiration. This is a upgrade operation, the file format will change. And the file won't be accessed correctly by older version (v1.2.16) of MMKV.- Parameters:
expireDurationInSecond
- the expire duration for all keys,ExpireNever
(0) means no default duration (aka each key will have it's own expire date)
-
disableAutoKeyExpire
public boolean disableAutoKeyExpire()
Disable auto key expiration. This is a downgrade operation.
-
getAll
public java.util.Map<java.lang.String,?> getAll()
Intentionally Not Supported. Because MMKV does type-eraser inside to get better performance.- Specified by:
getAll
in interfaceandroid.content.SharedPreferences
-
getString
@Nullable public java.lang.String getString(java.lang.String key, @Nullable java.lang.String defValue)
- Specified by:
getString
in interfaceandroid.content.SharedPreferences
-
putString
public android.content.SharedPreferences.Editor putString(java.lang.String key, @Nullable java.lang.String value)
- Specified by:
putString
in interfaceandroid.content.SharedPreferences.Editor
-
putString
public android.content.SharedPreferences.Editor putString(java.lang.String key, @Nullable java.lang.String value, int expireDurationInSecond)
-
getStringSet
@Nullable public java.util.Set<java.lang.String> getStringSet(java.lang.String key, @Nullable java.util.Set<java.lang.String> defValues)
- Specified by:
getStringSet
in interfaceandroid.content.SharedPreferences
-
putStringSet
public android.content.SharedPreferences.Editor putStringSet(java.lang.String key, @Nullable java.util.Set<java.lang.String> values)
- Specified by:
putStringSet
in interfaceandroid.content.SharedPreferences.Editor
-
putStringSet
public android.content.SharedPreferences.Editor putStringSet(java.lang.String key, @Nullable java.util.Set<java.lang.String> values, int expireDurationInSecond)
-
putBytes
public android.content.SharedPreferences.Editor putBytes(java.lang.String key, @Nullable byte[] bytes)
-
putBytes
public android.content.SharedPreferences.Editor putBytes(java.lang.String key, @Nullable byte[] bytes, int expireDurationInSecond)
-
getBytes
public byte[] getBytes(java.lang.String key, @Nullable byte[] defValue)
-
getInt
public int getInt(java.lang.String key, int defValue)
- Specified by:
getInt
in interfaceandroid.content.SharedPreferences
-
putInt
public android.content.SharedPreferences.Editor putInt(java.lang.String key, int value)
- Specified by:
putInt
in interfaceandroid.content.SharedPreferences.Editor
-
putInt
public android.content.SharedPreferences.Editor putInt(java.lang.String key, int value, int expireDurationInSecond)
-
getLong
public long getLong(java.lang.String key, long defValue)
- Specified by:
getLong
in interfaceandroid.content.SharedPreferences
-
putLong
public android.content.SharedPreferences.Editor putLong(java.lang.String key, long value)
- Specified by:
putLong
in interfaceandroid.content.SharedPreferences.Editor
-
putLong
public android.content.SharedPreferences.Editor putLong(java.lang.String key, long value, int expireDurationInSecond)
-
getFloat
public float getFloat(java.lang.String key, float defValue)
- Specified by:
getFloat
in interfaceandroid.content.SharedPreferences
-
putFloat
public android.content.SharedPreferences.Editor putFloat(java.lang.String key, float value)
- Specified by:
putFloat
in interfaceandroid.content.SharedPreferences.Editor
-
putFloat
public android.content.SharedPreferences.Editor putFloat(java.lang.String key, float value, int expireDurationInSecond)
-
getBoolean
public boolean getBoolean(java.lang.String key, boolean defValue)
- Specified by:
getBoolean
in interfaceandroid.content.SharedPreferences
-
putBoolean
public android.content.SharedPreferences.Editor putBoolean(java.lang.String key, boolean value)
- Specified by:
putBoolean
in interfaceandroid.content.SharedPreferences.Editor
-
putBoolean
public android.content.SharedPreferences.Editor putBoolean(java.lang.String key, boolean value, int expireDurationInSecond)
-
remove
public android.content.SharedPreferences.Editor remove(java.lang.String key)
- Specified by:
remove
in interfaceandroid.content.SharedPreferences.Editor
-
clear
public android.content.SharedPreferences.Editor clear()
- Specified by:
clear
in interfaceandroid.content.SharedPreferences.Editor
-
commit
@Deprecated public boolean commit()
Deprecated.- Specified by:
commit
in 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:
apply
in interfaceandroid.content.SharedPreferences.Editor
-
contains
public boolean contains(java.lang.String key)
- Specified by:
contains
in interfaceandroid.content.SharedPreferences
-
edit
public android.content.SharedPreferences.Editor edit()
- Specified by:
edit
in 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:
registerOnSharedPreferenceChangeListener
in 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:
unregisterOnSharedPreferenceChangeListener
in 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)
Deprecated.This method is deprecated. Use theinitialize(Context, String, LibLoader, MMKVLogLevel, MMKVHandler)
method instead.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.
-
-