茶楼 网站,dedecms 旅游网站模板,海阳网站建设,吕邵苍设计公司网站目录 一、MMKV介绍
1.特点和优势#xff1a; 2.使用指南#xff1a; 3.依赖包#xff1a;
二、MMKV的常用方法
1、初始化和获取实例#xff1a;
2、存储数据#xff1a; 3、读取数据
4、删除数据 5、其他操作#xff1a;
三、MMKV的使用例子 MainActivity#xff…目录 一、MMKV介绍
1.特点和优势 2.使用指南 3.依赖包
二、MMKV的常用方法
1、初始化和获取实例
2、存储数据 3、读取数据
4、删除数据 5、其他操作
三、MMKV的使用例子 MainActivity activity_main
运行结果
四、MMKV与SharedPreferences比较 一、MMKV介绍 MMKV是一种用于安卓平台的轻量级键值存储库它提供了一种高效、可靠的方式来在应用程序中存储和读取数据。
1.特点和优势
高性能相对于传统的SharedPreferences和SQLite等存储方式MMKV在性能方面表现出色。轻量级MMKV是一个小巧的库没有外部依赖易于集成到现有项目中。多线程支持MMKV具备良好的多线程支持可以在并发场景下安全地进行数据读写操作。易用性MMKV提供简单易用的API支持各种数据类型的存储和读取操作。数据安全MMKV采用内置的AES加密机制保护存储的数据安全。 2.使用指南
引入依赖在项目的build.gradle文件中添加MMKV的依赖项。初始化MMKV在应用程序的入口处初始化MMKV实例可以设置存储路径、加密密钥等参数。存储数据使用putXXX()方法将数据存储到MMKV中支持各种数据类型。读取数据使用getXXX()方法从MMKV中读取数据并进行相应的类型转换。其他操作MMKV还提供了删除特定键值、清空所有数据等操作。 3.依赖包
dependencies {implementation com.tencent:mmkv:1.2.10}
二、MMKV的常用方法
1、初始化和获取实例
String rootDir MMKV.initialize(context); // 初始化默认根目录
MMKV mmkv MMKV.defaultMMKV(); // 获取默认的MMKV实例2、存储数据
mmkv.encode(key, value); // 存储数据自动根据类型选择合适的方法
// 或者使用特定类型的存储方法
mmkv.putInt(intKey, intValue);
mmkv.putLong(longKey, longValue);
mmkv.putFloat(floatKey, floatValue);
mmkv.putBoolean(booleanKey, booleanValue);
mmkv.putString(stringKey, stringValue);
mmkv.putStringSet(setKey, stringSet);3、读取数据
Object value mmkv.decode(key); // 读取数据自动根据类型选择合适的方法
// 或者使用特定类型的读取方法
int intValue mmkv.getInt(intKey, defaultValue);
long longValue mmkv.getLong(longKey, defaultValue);
float floatValue mmkv.getFloat(floatKey, defaultValue);
boolean booleanValue mmkv.getBoolean(booleanKey, defaultValue);
String stringValue mmkv.getString(stringKey, defaultValue);
SetString stringSet mmkv.getStringSet(setKey, defaultSet);4、删除数据
mmkv.remove(key); // 删除指定键值对
mmkv.removeValueForKey(key); // 同上作用相同
mmkv.removeValuesForKeys(new String[]{key1, key2}); // 删除多个键值对
mmkv.clearAll(); // 清空所有数据5、其他操作
boolean contains mmkv.containsKey(key); // 判断是否包含指定的键
int count mmkv.count(); // 获取存储的数据总数
String[] allKeys mmkv.allKeys(); // 获取所有存储的键三、MMKV的使用例子 MainActivity
package com.example.mmkvdemo;import androidx.appcompat.app.AppCompatActivity;import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;import com.tencent.mmkv.MMKV;public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {EditText password, account;Button login, register;CheckBox mCheckBox;String Password, Account;MMKV mmkv;String TAG MainActivity;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();// 初始化MMKVString rootDir MMKV.initialize(this);Log.d(TAG, 路径 rootDir);// 读取保存的账号和密码mmkv MMKV.defaultMMKV();if (mmkv.contains(account) mmkv.contains(password)) {Account mmkv.decodeString(account, );Password mmkv.decodeString(password, );account.setText(Account);password.setText(Password);}}private void initView() {mCheckBox findViewById(R.id.Login_Remember);login findViewById(R.id.login_btn_login);register findViewById(R.id.login_btn_register);password findViewById(R.id.login_edit_pwd);account findViewById(R.id.login_edit_account);mCheckBox.setOnCheckedChangeListener(this);register.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, 点击了注册, Toast.LENGTH_LONG).show();}});login.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, 点击了登录, Toast.LENGTH_LONG).show();}});}Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked) {if (!TextUtils.isEmpty(password.getText().toString()) !TextUtils.isEmpty(account.getText().toString())) {// 存储数据Account account.getText().toString();Password password.getText().toString();mmkv.putString(account, Account); // 存储数据mmkv.putString(password, Password);}} else {// 清除保存的账号和密码mmkv.removeValuesForKeys(new String[]{account, password});}}
} activity_main
?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsandroid:orientationverticalandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentRelativeLayoutandroid:idid/login_viewandroid:layout_width400dpandroid:layout_height800dpandroid:layout_centerInParenttrueButtonandroid:idid/login_btn_registerandroid:layout_widthfill_parentandroid:layout_heightwrap_contentandroid:layout_belowid/login_btn_loginandroid:layout_alignParentStarttrueandroid:layout_alignParentLefttrueandroid:layout_marginTop10dpandroid:background#e52525android:text注册android:textColor#ffffffandroid:textSize20dp /Buttonandroid:idid/login_btn_loginandroid:layout_widthfill_parentandroid:layout_heightwrap_contentandroid:layout_belowid/login_edit_pwdandroid:layout_alignParentStarttrueandroid:layout_alignParentLefttrueandroid:layout_marginTop52dpandroid:background#545bcbandroid:text登录android:textColor#ffffffandroid:textSize20dp /EditTextandroid:idid/login_edit_pwdandroid:layout_width400dpandroid:layout_height60dpandroid:layout_belowid/login_edit_accountandroid:layout_alignParentStarttrueandroid:layout_alignParentLefttrueandroid:ems10android:hint请输入您的密码android:inputTypetextPasswordandroid:textColor#ffff00 /EditTextandroid:idid/login_edit_accountandroid:layout_width400dpandroid:layout_height60dpandroid:layout_alignParentStarttrueandroid:layout_alignParentLefttrueandroid:layout_marginTop20dpandroid:hint请输入您的用户名android:inputTypetextPersonNameandroid:textColor#ffff00 /CheckBoxandroid:idid/Login_Rememberandroid:layout_width100dpandroid:layout_height20dpandroid:layout_belowid/login_edit_pwdandroid:layout_alignParentStarttrueandroid:layout_alignParentLefttrueandroid:checkedfalseandroid:text记住密码android:textSize15dp /TextViewandroid:idid/login_text_change_pwdandroid:layout_width65dpandroid:layout_height20dpandroid:layout_belowid/login_edit_pwdandroid:layout_alignParentEndtrueandroid:layout_alignParentRighttrueandroid:text忘记密码android:textSize15dp //RelativeLayout
/LinearLayout运行结果 四、MMKV与SharedPreferences比较
MMKV相对于SharedPreferencessp有以下几个优势
1. 性能更好MMKV使用了自定义的键值存储引擎底层采用了mmap映射文件的方式避免了内存拷贝和序列化/反序列化的开销。相比之下SharedPreferences是基于XML文件的存储每次读写都需要进行IO操作性能较低。
2. 存储容量更大MMKV支持将数据存储在独立的文件中并且可以设置单个文件的最大大小而SharedPreferences则是将数据存储在一个XML文件中当存储的数据量较大时性能会下降。
3. 多进程并发安全MMKV通过文件锁机制来实现多进程并发安全访问避免了数据错乱和冲突的问题。而SharedPreferences没有提供多进程并发安全的保证当多个进程同时对同一个SharedPreferences文件进行操作时可能会导致数据异常。
4. 支持自定义加密MMKV支持自定义的加密器可以对存储的数据进行加密保护增加数据的安全性。而SharedPreferences不直接支持加密需要开发者手动对存储的数据进行加密处理。
5. API更简洁易用MMKV提供了简洁易用的API使用起来更加方便快捷可以直接存储各种类型的数据无需手动进行类型转换。而SharedPreferences需要手动进行类型转换和键值的拼接。 总的来说MMKV相对于SharedPreferences在性能、存储容量、多进程并发安全性和加密等方面有着明显的优势特别适合用于高性能要求、大数据量存储或多进程场景下的数据存储需求。