0

我正在制作一个演示项目,以使用 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);
}

我错过了什么吗?感谢帮助 :)

4

1 回答 1

-1

如果您已正确设置 Firebase 控制台,建议您检查手机上是否有最新的Google Play 服务可用并正常工作。

于 2016-12-29T07:52:45.307 回答