我正在制作一个演示项目,以使用 Firebase 远程配置更改简单 TextView 的颜色。但是函数 mRemoteConfig.fetch(cacheExpiration) .addOnCompleteListener... 根本不会触发。文本颜色不会改变。
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
initRemoteConfig();
}
private void initRemoteConfig() {
mRemoteConfig = FirebaseRemoteConfig.getInstance();
HashMap<String, Object> defaults = new HashMap<>();
defaults.put("color_primary", getString(R.string.color_primary));
mRemoteConfig.setDefaults(defaults);
FirebaseRemoteConfigSettings remoteConfigSettings = new FirebaseRemoteConfigSettings.Builder()
.setDeveloperModeEnabled(true)
.build();
mRemoteConfig.setConfigSettings(remoteConfigSettings);
fetchRemoteConfigValues();
}
private void fetchRemoteConfigValues() {
long cacheExpiration = 3600;
//expire the cache immediately for development mode.
if (mRemoteConfig.getInfo().getConfigSettings().isDeveloperModeEnabled()) {
cacheExpiration = 0;
}
mRemoteConfig.fetch(cacheExpiration)
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> task) {
if (task.isSuccessful()) {
// task successful. Activate the fetched data
mRemoteConfig.activateFetched();
setupView();
} else {
//task failed
}
}
});
}
private void setupView() {
setTextColor();
}
private void setTextColor() {
boolean isPromoOn = true;
int color = isPromoOn ? Color.parseColor(mRemoteConfig.getString("color_primary")) :
ContextCompat.getColor(this, R.color.color_primary);
text.setTextColor(color);
}
我错过了什么吗?感谢帮助 :)