DataStore 提供两种不同的实现:Preferences DataStore 和 Proto DataStore。
implementation "androidx.datastore:datastore:1.0.0"
implementation "androidx.datastore:datastore-rxjava2:1.0.0"
RxDataStore<Preferences> dataStore =
new RxPreferenceDataStoreBuilder(this,"settings").build();
//获取key为open的值
Preferences.Key<Integer> EXAMPLE_COUNTER = PreferencesKeys.intKey("open");
dataStore
.data()
.map(new Function<Preferences, Integer>() {
@Override
public Integer apply(Preferences preferences) throws Exception {
Integer integer = preferences.get(EXAMPLE_COUNTER);
return integer;
}})
.subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
}
});
Single<Preferences> updateResult = dataStore.updateDataAsync(prefsIn -> {
MutablePreferences mutablePreferences = prefsIn.toMutablePreferences();
mutablePreferences.set(EXAMPLE_COUNTER, 1);
return Single.just(mutablePreferences);
});
implementation "androidx.datastore:datastore:1.0.0"
implementation "androidx.datastore:datastore-rxjava2:1.0.0"
// 使用proto3
syntax = "proto3";
// 改为自己的包名
option java_package = "cn.jn.mytest";
option java_multiple_files = true;
//内容定义 UserPreferences 为类名
//字段格式为 类型 字段名 = 编号 ,编号需要唯一。
message UserPreferences {
string name = 1;
int32 age = 2;
float weight = 3;
float tall = 4;
int32 sex = 5;
}
配置编译插件
plugins {
.........
id 'com.google.protobuf' version '0.9.4' apply false
}
引入插件
plugins {
......
id 'com.google.protobuf'
}
导入编译依赖
implementation "com.google.protobuf:protobuf-javalite:3.14.0"
配置build.gradle(app)
android {
.......
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.8.0'
}
generateProtoTasks {
all().each { task ->
task.builtins {
java {
option "lite"
}
}
}
}
}
}
创建Serializer,T是.proto文件定义类型。
private static class UserSerializer implements Serializer<UserPreferences> {
@Override
public UserPreferences getDefaultValue() {
return UserPreferences.getDefaultInstance();
}
@Nullable
@Override
public Object readFrom(@NonNull InputStream inputStream, @NonNull Continuation<? super UserPreferences> continuation) {
try {
return UserPreferences.parseFrom(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Nullable
@Override
public Object writeTo(UserPreferences userPreferences, @NonNull OutputStream outputStream, @NonNull Continuation<? super Unit> continuation) {
try {
userPreferences.writeTo(outputStream);
return userPreferences;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
RxDataStore<UserPreferences> dataStore =
new RxDataStoreBuilder<>(this, "user.pb", new UserSerializer()).build();
读取数据
//从 Proto DataStore 读取内容
dataStore.data().map(UserPreferences::getName).subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
Log.d("UserPreferences", "Name:" + s);
}
});
写入数据
//将内容写入 Proto DataStore
dataStore.updateDataAsync(userPreferences -> Single.just(userPreferences.toBuilder().setName("XX").build()))
.subscribe(new Consumer<UserPreferences>() {
@Override
public void accept(UserPreferences userPreferences) throws Exception {
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
}
});