Hawk 是一个非常便捷的数据库 . 操作数据库只需一行代码 , 能存任何数据类型 .
github 地址: https://github.com/orhanobut/hawk
一、概念
SharedPreferences的使用大家应该非常熟悉啦。这是一种轻量级的存储简单配置信息的存储机制,以key-value的形式保存数据。
这里介绍一款基于SharedPreferences的的存储框架,是由Android开发大神Orhan Obut开源维护的,
它使用:
AES 加密
能选择使用SharedPreferences 或者 SQLite
Gson解析 (文章下方的Simple中有替换fastJson的版本)
提供:
安全数据持久化
能存储任何类型
二、用法
添加依赖
compile "com.orhanobut:hawk:2.0.1"
初始化
Hawk.init(context).build();
//基本数据类型
Hawk.put("position","zz");
Hawk.get(“position”);
二、用法源码分析
put:
HawkUtils.checkNull("Key", key);
log("Hawk.put -> key: " + key + ", value: " + value);
// If the value is null, delete it
if (value == null) {
log("Hawk.put -> Value is null. Any existing value will be deleted with the given key");
return delete(key);
}
// 1. Convert to text
String plainText = converter.toString(value);
log("Hawk.put -> Converted to " + plainText);
if (plainText == null) {
log("Hawk.put -> Converter failed");
return false;
}
// 2. Encrypt the text
String cipherText = null;
try {
cipherText = encryption.encrypt(key, plainText);
log("Hawk.put -> Encrypted to " + cipherText);
} catch (Exception e) {
e.printStackTrace();
}
if (cipherText == null) {
log("Hawk.put -> Encryption failed");
return false;
}
// 3. Serialize the given object along with the cipher text
String serializedText = serializer.serialize(cipherText, value);
log("Hawk.put -> Serialized to" + serializedText);
if (serializedText == null) {
log("Hawk.put -> Serialization failed");
return false;
}
// 4. Save to the storage
if (storage.put(key, serializedText)) {
log("Hawk.put -> Stored successfully");
return true;
} else {
log("Hawk.put -> Store operation failed");
return false;
}
get:
log("Hawk.get -> key: " + key);
if (key == null) {
log("Hawk.get -> null key, returning null value ");
return null;
}
// 1. Get serialized text from the storage
String serializedText = storage.get(key);
log("Hawk.get -> Fetched from storage : " + serializedText);
if (serializedText == null) {
log("Hawk.get -> Fetching from storage failed");
return null;
}
// 2. Deserialize
DataInfo dataInfo = serializer.deserialize(serializedText);
log("Hawk.get -> Deserialized");
if (dataInfo == null) {
log("Hawk.get -> Deserialization failed");
return null;
}
// 3. Decrypt
String plainText = null;
try {
plainText = encryption.decrypt(key, dataInfo.cipherText);
log("Hawk.get -> Decrypted to : " + plainText);
} catch (Exception e) {
log("Hawk.get -> Decrypt failed: " + e.getMessage());
}
if (plainText == null) {
log("Hawk.get -> Decrypt failed");
return null;
}
// 4. Convert the text to original data along with original type
T result = null;
try {
result = converter.fromString(plainText, dataInfo);
log("Hawk.get -> Converted to : " + result);
} catch (Exception e) {
log("Hawk.get -> Converter failed");
}
adb命令查看sharedpreferences
adb shell run-as com.example.android //对应包名
cd shared_prefs
注意点:APP升级过程中 修改包名称导致反序列化失败,get数据为null。
参考:https://www.cnblogs.com/zhangqie/p/10512215.html
今天的文章hawk a product_hawk英文怎么读分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/65703.html
