SoFunction
Updated on 2025-04-10

Records of Android using MMKV

Preface

I heard that Tencent’s mmkv is very coquettish and can replace SharedPreferences. It is mainly used to save settings, such as serial port number, baud rate, camera preview angle, etc. Let’s try it O(∩_∩)O haha~

1. Dependency introduction, app

implementation ':mmkv-static:1.0.23'

2. Encapsulation class

import ;

import ;

public class SharedPreferencesManager {


    private static MMKV kv;

    public static void init(Context context) {
        String rootDir = (context);
        ("mmkv root: " + rootDir);
        kv = ();
    }

    public static void putBoolean(String key, boolean value) {
        (key, value);
    }

    public static Boolean getBoolean(String key) {
        return (key, false);
    }

    public static Boolean getBoolean(String key, boolean defValue) {
        return (key, defValue);
    }

    public static void putInteger(String key, int value) {
        (key, value);
    }

    public static int getInteger(String key) {
        return (key, 1);
    }

    public static int getInteger(String key, int defValue) {
        return (key, defValue);
    }

    public static void putString(String key, String value) {
        (key, value);
    }

    public static String getString(String key) {
        return (key, "");
    }

    public static String getString(String key, String defaultValue) {
        return (key, defaultValue);
    }


}

3. Initialize in a custom application

public class XXApplication extends Application {

			   @Override
   			   public void onCreate() {
        	   ();
 				    /*Initialize SharedPreferences*/
            (this);
		
    		}
}

4. Use it with your heart

        String mcuPath = (Constants.SP_KEY_COM_MCU, Constants.DEFAULT_COM_MCU);
      (Constants.SP_KEY_COM_MCU, mcuPath);

This is all about this article about Android using MMKV. For more related content on Android using MMKV, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!