2024年Android最全Android存储:轻松掌握MMKV(1),2024年最新零基础学android

文末

初级工程师拿到需求会直接开始做,然后做着做着发现有问题了,要么技术实现不了,要么逻辑有问题。

而高级工程师拿到需求会考虑很多,技术的可行性?对现有业务有没有帮助?对现有技术架构的影响?扩展性如何?等等…之后才会再进行设计编码阶段。

而现在随着跨平台开发,混合式开发,前端开发之类的热门,Android开发者需要学习和掌握的技术也在不断的增加。

通过和一些行业里的朋友交流讨论,以及参考现在大厂面试的要求。我们花了差不多一个月时间整理出了这份Android高级工程师需要掌握的所有知识体系。你可以看下掌握了多少。

混合式开发,微信小程序。都是得学会并且熟练的

这些是Android相关技术的内核,还有Java进阶

高级进阶必备的一些技术。像移动开发架构项目实战等

Android前沿技术;包括了组件化,热升级和热修复,以及各种架构跟框架的详细技术体系

以上即是我们整理的Android高级工程师需要掌握的技术体系了。可能很多朋友觉得很多技术自己都会了,只是一些新的技术不清楚而已。应该没什么太大的问题。

而这恰恰是问题所在!为什么别人高级工程师能年限突破30万,而你只有十几万呢?

就因为你只需补充你自己认为需要的,但并不知道企业需要的。这个就特别容易造成差距。因为你的技术体系并不系统,是零碎的,散乱的。那么你凭什么突破30万年薪呢?

我这些话比较直接,可能会戳到一些人的玻璃心,但是我知道肯定会对一些人起到点醒的效果的。而但凡只要有人因为我的这份高级系统大纲以及这些话找到了方向,并且付出行动去提升自我,为了成功变得更加努力。那么我做的这些就都有了意义。

喜欢的话请帮忙转发点赞一下能让更多有需要的人看到吧。谢谢!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

<TextView        
    android:id="@+id/tv"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text="0"  
    android:textSize="28sp"  
    app:layout_constraintBottom_toBottomOf="parent"  
    app:layout_constraintEnd_toEndOf="parent"  
    app:layout_constraintStart_toStartOf="parent"  
    app:layout_constraintTop_toTopOf="parent" />  
    
<com.google.android.material.floatingactionbutton.FloatingActionButton  
    android:id="@+id/fab_delete"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:layout_marginBottom="12sp"  
    android:layout_marginEnd="24dp"  
    android:contentDescription="delete"  
    android:src="@drawable/ic_baseline_delete_forever_24"  
    app:background="@android:color/holo_red_dark"  
    app:backgroundTint="@android:color/holo_red_dark"  
    app:layout_constraintBottom_toTopOf="@+id/fab_add"  
    app:layout_constraintEnd_toEndOf="parent"  
    app:tint="@color/white" />

<com.google.android.material.floatingactionbutton.FloatingActionButton        
    android:id="@+id/fab_add"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:layout_margin="24dp"  
    android:src="@drawable/ic_baseline_exposure_plus_1_24"  
    app:backgroundTint="@color/black"  
    app:background="@color/black"  
    app:tint="@color/white"  
    app:layout_constraintBottom_toBottomOf="parent"  
    app:layout_constraintEnd_toEndOf="parent"  
    android:contentDescription="add" />  

</androidx.constraintlayout.widget.ConstraintLayout>


activity代码如下:



class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {  
    super.onCreate(savedInstanceState)  
    setContentView(R.layout.activity_main)  
	  
    val tv = findViewById<TextView>(R.id.tv)  
    val add = findViewById<FloatingActionButton>(R.id.fab_add)
    val delete = findViewById<FloatingActionButton>(R.id.fab_delete)  
}  

}


我们的最终目的是通过不断点击 fab\_add 使得 TextView 内的数字不断+1,然后点击 fab\_delete 将 TextView 内的数字清零。


![](https://img-blog.csdnimg.cn/img_convert/196035dd8d87ededbda388d221d2fffc.webp?x-oss-process=image/format,png)


##### 开始使用


###### 添加依赖


Github:Tencent/MMKV: An efficient, small mobile key-value storage framework developed by WeChat. Works on Android, iOS, macOS, Windows, and POSIX. (github.com)



dependencies {
// 将1.3.1替换为最新的版本
implementation ‘com.tencent:mmkv:1.3.1’
}


###### 初始化


实现应用的存储功能通常在App启动时就要开始初始化其对象,所以我们在自定义 Application 内对 MMKV 初始化。



class GlobalApplication : Application() {
override fun onCreate() {
super.onCreate()
// 初始化
MMKV.initialize(this)
}
}


MMKV 默认把文件存放在$(FilesDir)/mmkv/目录。可以在 MMKV初始化时自定义根目录



String dir = getFilesDir().getAbsolutePath() + “/mmkv”;
String rootDir = MMKV.initialize(dir);


MMKV 为我们提供一个全局的实例,可以直接使用



// 获取 MMKV 默认全局实例,一般选用这个,本文也是选择这个进行实例创建
val mmkv = MMKV.defaultMMKV()

// 根据设置 id 来自定义 MMKV 对象。比如根据业务来区分的存取实例
val mmkv = MMKV.mmkvWithID(“ID”)

// 开启多进程访问。默认是单线程
val mmkv = MMKV.mmkvWithID(“ID”,MMKV.MULTI_PROCESS_MODE)


与 SP 类似,我们需要定义一个 key 值用于读取写入 MMKV 中的数据



val key = “number”


###### 支持类型


支持以下 Java 语言基础类型:


* boolean、int、long、float、double、byte[]
* 支持以下 Java 类和容器:
* String、Set< String >
* 任何实现了 Parcelable 的类型


###### 读取数据


MMKV 为用户读取数据提供了两种类型的函数,其中T为上述的 MMVK 支持类型


* `decodeT(String key)`:读取指定 key 中的值
* `decodeT(String key, T defaultValue)`:读取指定 key 中的值,如果该 key 不存在则赋予默认值并返回。


其中需要注意的是,第一个函数虽然没有明面上赋予默认值,但查看源码,可以发现如果 key 不存在时 MMKV 也会为其设置固定默认值并返回,以 `decodeInt` 为例子,MMKV 会设置初始默认值 0



public int decodeInt(String key) {
return this.decodeInt(this.nativeHandle, key, 0);
}
public int decodeInt(String key, int defaultValue) {
return this.decodeInt(this.nativeHandle, key, defaultValue);
}


以下为其他类型 MMKV 赋予的初始默认值


![](https://img-blog.csdnimg.cn/img_convert/ec34e2057e471071811d077313f6d43f.webp?x-oss-process=image/format,png)


在计数器案例中,我们选择 int 类型数据



// 初始化 TextView 的值
// 获取存储在本地的 number 值,如果不存在 mmkv 返回默认值为 0
tv.text = mmkv.decodeInt(key).toString()


###### 写入数据


MMKV 提供了便利的 API 可以直接写入数据: `public boolean encode(String key, T value)`  
 其中 T 为 MMKV 可支持的类型,在上文已经提过了。 现在我们就可以直接实现点击 fab\_add 按钮来修改数据



add.setOnClickListener {
// 由于点击 FloatingActionButton 值会增加,所以 value 值在原先存储值的基础上 +1
val value = mmkv.decodeInt(key) + 1
// 将数据写入 MMVK
mmkv.encode(key, value)
// 更新 TextView 的值
tv.text = value.toString()
}


###### 删除数据


删除单个key:`removeValueForKey(String key)`  
 删除多个key:`removeValuesForKeys(String[] arrKeys)`  
 清空所有数据:`clearAll()`  
 在这里因为我们只有一个 key ,所以直接使用`removeValueForKey`来清除我们的数据,同时更新 TextView 的 UI



delete.setOnClickListener {
// 清除数据
mmkv.removeValueForKey(key)
// 更新 TextView 数据
tv.text = mmkv.decodeInt(key, 0).toString()
}


###### 从 SharedPreferences 迁移到 MMKV


为了演示怎么迁移,我们重头再来,在**准备部分的代码**基础上临时创建 SharedPreferences 储存数据。



如何做好面试突击,规划学习方向?

面试题集可以帮助你查漏补缺,有方向有针对性的学习,为之后进大厂做准备。但是如果你仅仅是看一遍,而不去学习和深究。那么这份面试题对你的帮助会很有限。最终还是要靠资深技术水平说话。

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。建议先制定学习计划,根据学习计划把知识点关联起来,形成一个系统化的知识体系。

学习方向很容易规划,但是如果只通过碎片化的学习,对自己的提升是很慢的。

我们搜集整理过这几年字节跳动,以及腾讯,阿里,华为,小米等公司的面试题,把面试的要求和技术点梳理成一份大而全的“ Android架构师”面试 Xmind(实际上比预期多花了不少精力),包含知识脉络 + 分支细节。

img

我们在搭建这些技术框架的时候,还整理了系统的高级进阶教程,会比自己碎片化学习效果强太多

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

1715707560197)]

我们在搭建这些技术框架的时候,还整理了系统的高级进阶教程,会比自己碎片化学习效果强太多

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 15
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.省,市,区三级关联 2.地址数据本地化 3.自定义对话框 4.支付从xml文件,或对象文件加载 部分代码: package com.demo.selector; import java.io.InputStream; import com.address.selector.City; import com.address.selector.Country; import com.address.selector.Province; import com.address.selector.Region; import com.demo.address.R; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; import android.widget.AdapterView.OnItemSelectedListener; public class AddressSelectorDialog extends BaseDialog { /**省类型*/ private static final int INDEX_PROVINCE = 1; /**城市类型*/ private static final int INDEX_CITY = 2; /**区县类型*/ private static final int INDEX_REGION = 3; private static Country china = null; private Spinner spnProvince = null; private Spinner spnCity = null; private Spinner spnRegion = null; private ArrayAdapter dptProvince = null; private ArrayAdapter dptCity = null; private ArrayAdapter dptRegion = null; private SpinnerSelectedListener proListener = new SpinnerSelectedListener(); private SpinnerSelectedListener cityListener = new SpinnerSelectedListener(); private SpinnerSelectedListener regionListener = new SpinnerSelectedListener(); //当前选择地区的索引值 private int provinceId = -1; private int cityId = -1; private int regionId = -1; /**选择按钮*/ private Button btnSelect = null; /**回调用事件*/ private OnSelectorCancel selectorCancel = null; @Override public void init(Context context) { // TODO Auto-generated method stub this.context = context; dialog = new Dialog(this.context,R.style.CustomDialog); dialog.setContentView(R.layout.adress_selector); spnProvince = (Spinner)dialog.findViewById(R.id.spin_province); spnProvince.setTag(new Integer(INDEX_PROVINCE)); spnCity = (Spinner)dialog.findViewById(R.id.spin_city); spnCity.setTag(new Integer(INDEX_CITY)); spnRegion = (Spinner)dialog.findViewById(R.id.spin_region); spnRegion.setTag(new Integer(INDEX_REGION)); btnSelect = (Button)dialog.findViewById(R.id.btn_select); btnSelect.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub selectAddress(); } }); //当对话框隐藏后调用[回调事件] dialog.setOnCancelListener(new OnCancelListener(){ @Override public void onCancel(DialogInterface arg0) { // TODO Auto-generated method stub if (selectorCancel != null) { String proName = ""; String cityName = ""; String regName = ""; try { Province tmpPro = china.provinceLst.get(provinceId); proName = tmpPro.areaName; City tmpCity = tmpPro.cityLst.get(cityId); cityName = tmpCity.areaName; Region tmpReg = tmpCity.regionLst.get(regionId); regName = tmpReg.areaName; } catch (Exception e) { android.util.Log.e("selectorCancel???", e.getMessage()); } selectorCancel.onSelectorResult(proName, cityName, regName); } } }); //加载数据 if (china == null) { loadCountryAddressData(); } //加载完后,初始化适配器 initAdapters(); } @Override public void update(Object obj) { // TODO Auto-generated method stub } private void loadCountryAddressData() { try { /** * 反序化的包名,类名,版本ID必须一致,否则ClassNotFoundException */ InputStream stream = this.context.getAssets().open("china-city.obj"); china = Country.loadFromStream(stream); //mHandler.sendEmptyMessage(0); } catch (Exception e) { android.util.Log.e("error", e.getMessage()); } } class SpinnerSelectedListener implements OnItemSelectedListener{ @Override public void onItemSelected (AdapterView parent, View view, int position, long id) { // TODO Auto-generated method stub //android.util.Log.i("", String.format("[pos=%d]",position)); int index = (Integer)parent.getTag(); //ArrayAdapter obj = (ArrayAdapter)(parent.getAdapter()); //String value = obj.getItem(position); //android.util.Log.i("", "value=" + value); if (index == INDEX_PROVINCE) { provinceId = position; if (isValideProvinceIndex(provinceId)) { //改变城市选择 Province tmpProvince = china.provinceLst.get(provinceId); String lstNames[] = tmpProvince.listNames(); if (lstNames == null) { //没有城市,当然没区县 provinceId = -1; spnCity.setVisibility(View.INVISIBLE); regionId = -1; spnRegion.setVisibility(View.INVISIBLE); return ; } else { if (spnCity.getVisibility() == View.INVISIBLE) { spnCity.setVisibility(View.VISIBLE); } if (spnRegion.getVisibility() == View.INVISIBLE) { spnRegion.setVisibility(View.INVISIBLE); } } dptCity = new ArrayAdapter(context, android.R.layout.simple_spinner_item, lstNames); dptCity.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spnCity.setAdapter(dptCity); spnCity.setOnItemSelectedListener(cityListener); } } else if (index == INDEX_CITY) { cityId = position; if (isValideCityIndex(provinceId,cityId)) { //改变区选择 Province tmpProvince = china.provinceLst.get(provinceId); City tmpCity = tmpProvince.cityLst.get(cityId); String lstNames[] = tmpCity.listNames(); if (lstNames == null) { //没有区县 regionId = -1; spnRegion.setVisibility(View.INVISIBLE); return ; } else if (spnRegion.getVisibility() == View.INVISIBLE) { spnRegion.setVisibility(View.VISIBLE); } dptRegion = new ArrayAdapter(context, android.R.layout.simple_spinner_item, lstNames); dptRegion.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spnRegion.setAdapter(dptRegion); spnRegion.setOnItemSelectedListener(regionListener); } } else if (index == INDEX_REGION) { regionId = position; } } @Override public void onNothingSelected(AdapterView parent) { // TODO Auto-generated method stub } } private void initAdapters() { dptProvince = new ArrayAdapter(this.context, android.R.layout.simple_spinner_item, china.listNames()); //设置下拉列表的风格 dptProvince.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //设置适配器 spnProvince.setAdapter(dptProvince); //设置侦听事件 spnProvince.setOnItemSelectedListener(proListener); } private boolean isValideProvinceIndex(int index) { return (china != null) && (index >= 0 && index = 0) && (china.provinceLst.get(provinceIndex).cityLst.size() > cityIndex); } return false; } private void selectAddress() { hide(); } /** * 调协回调事件 * @param selectorCancel */ public void setOnSelectorCancel(OnSelectorCancel selectorCancel) { this.selectorCancel = selectorCancel; } /** * 选择器关闭后,调用回调事件接口 */ public interface OnSelectorCancel { /** * @param proName 省份名 * @param cityName 城市名 * @param regName 区县名 */ public void onSelectorResult(String proName,String cityName,String regName); }; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值