【问题标题】:How to change colorPrimary, colorPrimaryDark, and so on by Firebase Remote Config? [closed]如何通过 Firebase 远程配置更改 colorPrimary、colorPrimaryDark 等? [关闭]
【发布时间】:2016-09-20 12:39:42
【问题描述】:

我想要做的是在 Firebase 的 RemoteConfig 中保存 colorPrimay、colorDarkPrimary 等值,这样我就可以从远程配置中更改应用程序的颜色,我做了以下操作

更新: 我想要做的是在 Firebase 的 RemoteConfig 中保存 colorPrimay、colorDarkPrimary 等值,这样我就可以从远程配置中更改应用程序的颜色。我怎么能这样做?

更新:

我尝试过的是这样的

<--!remote_config_default.xml!-->


<defaultsMap>
<entry>
    <key>primaryColor</key>
    <value>#9c27b0</value>
</entry>
<entry>
    <key>colorPrimaryDark</key>
    <value>#7b1fa2</value>
</entry>
<entry>
    <key>colorAccent</key>
    <value>#FF4081</value>
</entry>
<entry>welcomeMessage</entry>
<value>Connection Failed</value>

然后像这样获取

    void applyRemoteConfig() {
        mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
        mFirebaseRemoteConfig.setDefaults(R.xml.remote_config_default);
        // cacheExpirationSeconds is set to cacheExpiration here, indicating that any previously
// fetched and cached config would be considered expired because it would have been fetched
// more than cacheExpiration seconds ago. Thus the next fetch would go to the server unless
// throttling is in progress. The default expiration duration is 43200 (12 hours).
        int cacheExpiration = 1000;
        final String TAG = "Riddles";
        mFirebaseRemoteConfig.fetch(cacheExpiration)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            Log.d(TAG, "Fetch Succeeded");
                            // Once the config is successfully fetched it must be activated before newly fetched
                            // values are returned.
                            mFirebaseRemoteConfig.activateFetched();
                        } else {
                            Log.d(TAG, "Fetch failed");
                            String message = mFirebaseRemoteConfig.getString("welcomeMessage");
                            displayWelcomeMessage(message);
                            Log.d(TAG, message);
                        }
                    }
                });

        String message = mFirebaseRemoteConfig.getString("welcomeMessage");
        displayWelcomeMessage(message);
        Log.d(TAG, message);


    }

但是

mFirebaseRemoteConfig.getString("colorPrimary");

mFirebaseRemoteConfig.getString("colorDarkPrimary");

mFirebaseRemoteConfig.getString("welcomeMessage");

正在返回 Null。

【问题讨论】:

    标签: android firebase firebase-remote-config


    【解决方案1】:

    您可以将其添加到您的远程配置文件中。

    <!-- color entries -->
        <entry>
            <key>color_primary</key>
            <value>#3F51B5</value>
        </entry>
        <entry>
            <key>color_primary_dark</key>
            <value>#303F9F</value>
        </entry>  
    

    要动态更改应用的颜色,请同时使用远程配置提供的标志并使用fetch() 更改它。详细实现refer to this.

    【讨论】:

    • 是的,我已经做到了,看看我所做的更新
    • @NgimaSherpa 你的获取成功了吗?您可以检查记录器以查看获取是否成功?
    • 它说获取失败
    • 虽然无法获取,但应该是默认值吧?
    • @NgimaSherpa 你从哪里调用 fetch()?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多