_/
public static final String PHONE=“phone”;
/_*
* 姓名
* valueType[Boolean]
* 默认值:false
*/
public static final String IS_MARRIED=“is_married”;
}
2.创建SharedPreferences的帮助类
这个类主要作为各个SharedPreferences的生产工厂使用,并进一步提供简化的提交,获取值的方法。
public class SharedPreferencesHelper {
private static final String SP_NAME_USER = “sp_name_user”;//用户相关的SP
/**
-
用户相关Sp
-
相关的key见{@link SpKeyUser}
*/
public static SharedPreferences getUserSp() {
return MyApplication.getInstances().getSharedPreferences(SP_NAME_USER, Context.MODE_PRIVATE);
}
/**
-
用户相关Sp
-
相关的key见{@link SpKeyDefault}
*/
public static SharedPreferences getDefaultSp() {
return PreferenceManager.getDefaultSharedPreferences(MyApplication.getInstances());
}
//sharedPreferences 是否为空
public static boolean isEmpty(SharedPreferences sp) {
return sp == null || sp.getAll() == null || 0 == sp.getAll().size();
}
/**
- 默认值为""
*/
public static String getString(SharedPreferences sp, String key) {
return sp.getString(key, Key.NIL);
}
/**
-
具有默认值
-
@param defValue 默认值
*/
public static String getString(SharedPreferences sp, String key, String defValue) {
return sp.getString(key, defValue);
}
/**
- 默认值为0L
*/
public static long getLong(SharedPreferences sp, String key) {
return sp.getLong(key, 0L);
}
/**
- 默认值为0
*/
public static int getInt(SharedPreferences sp, String key) {
return sp.getInt(key, 0);
}
public static void setPreference(SharedPreferences sp, String key, int value) {
sp.edit().putInt(key, value).apply();
}
public static void setPreference(SharedPreferences sp, String key, String value) {
sp.edit().putString(key, value).apply();
}
public static void setPreference(SharedPreferences sp, String key, boolean value) {
sp.edit().putBoolean(key, value).apply();
}
//批量put数据
public static void setPreferenceWithList(SharedPreferences sp, List spItemList) {
if (sp == null || spItemList == null || spItemList.isEmpty()) {
return;
}
Editor spEditor = sp.edit();
for (SpItem item : spItemList) {
setSpItem(spEditor, item);
}
spEditor.apply();
}
public static void setPreference(SharedPreferences sp, String key, long value) {
sp.edit().putLong(key, value).apply();
}
/**
- 默认值为false
*/
public static boolean getBoolean(SharedPreferences sp, String key) {
return sp.getBoolean(key, false);
}
/**
- 自定义默认值
*/
public static boolean getBoolean(SharedPreferences sp, String key, boolean defValue) {
return sp.getBoolean(key, defValue);
}
/**
- 清除某一个key
*/
public static void remove(SharedPreferences sp, String key) {
if (sp == null || TextUtils.isEmpty(key)) {
return;
}
sp.edit().remove(key).apply();
}
//值类型(String:0,int:1,long:2,float:3,boolean:4)
private static final int VALUE_TYPE_STRING = 0;
private static final int VALUE_TYPE_INT = 1;
private static final int VALUE_TYPE_LONG = 2;
private static final int VALUE_TYPE_FLOAT = 3;
private static final int VALUE_TYPE_BOOLEAN = 4;
//Sp提交多个时使用,用于描述每个提交项
public static class SpItem {
private String mKey;
private T mValue;
private int mValueType;
public SpItem(String key, T value) {
this.mKey = key;
this.mValue = value;
this.mValueType = initValueType(value);
}
private int initValueType(T t) {
int typ = -1;
if (t instanceof String) {
typ = VALUE_TYPE_STRING;
} else if (t instanceof Integer) {
typ = VALUE_TYPE_INT;
} else if (t instanceof Long) {
typ = VALUE_TYPE_LONG;
} else if (t instanceof Float) {
typ = VALUE_TYPE_FLOAT;
} else if (t instanceof Boolean) {
typ = VALUE_TYPE_BOOLEAN;
}
return typ;
}
}
//按类型put数据
private static void setSpItem(Editor spEditor, SpItem spItem) {
if (spItem == null || spEditor == null) {
return;
}
switch (spItem.mValueType) {
case VALUE_TYPE_STRING:
spEditor.putString(spItem.mKey, (String) spItem.mValue);
break;
case VALUE_TYPE_INT:
spEditor.putInt(spItem.mKey, (Integer) spItem.mValue);
尾声
如果你想成为一个优秀的 Android 开发人员,请集中精力,对基础和重要的事情做深度研究。
对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。 整理的这些架构技术希望对Android开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。
这里,笔者分享一份从架构哲学的层面来剖析的视频及资料分享给大家梳理了多年的架构经验,筹备近6个月最新录制的,相信这份视频能给你带来不一样的启发、收获。
PS:之前因为秋招收集的二十套一二线互联网公司Android面试真题 (含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)
架构篇
《Jetpack全家桶打造全新Google标准架构模式》
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
…(img-BB0No5DY-1714204366514)]
架构篇
《Jetpack全家桶打造全新Google标准架构模式》
[外链图片转存中…(img-mNPduYVM-1714204366515)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!