我刚开始在 Firebase 控制台中使用远程配置。这是一个很棒的工具,它工作正常,但我不知道如何使用控制台中的参数将不同的图像(drawables)“推送”到我的应用程序。这甚至可能吗?如果可以,我如何在我的应用程序中使用它?
我当前的 MainActivity:
public class MainActivity extends AppCompatActivity {
private static final String TEAM1 = "team1";
private static final String TEAM2 = "team2";
private FirebaseRemoteConfig mFirebaseRemoteConfig;
private TextView mTeam1Text;
private TextView mTeam2Text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTeam1Text = (TextView) findViewById(R.id.team1);
mTeam2Text = (TextView) findViewById(R.id.team2);
Button fetchButton = (Button) findViewById(R.id.button);
fetchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fetchDiscount();
}
});
mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
.setDeveloperModeEnabled(BuildConfig.DEBUG)
.build();
mFirebaseRemoteConfig.setConfigSettings(configSettings);
mFirebaseRemoteConfig.setDefaults(R.xml.remote_config_defaults);
fetchDiscount();
}
private void fetchDiscount() {
mTeam1Text.setText(mFirebaseRemoteConfig.getString(TEAM1));
mTeam2Text.setText(mFirebaseRemoteConfig.getString(TEAM2));
long cacheExpiration = 3600;
if (mFirebaseRemoteConfig.getInfo().getConfigSettings().isDeveloperModeEnabled()) {
cacheExpiration = 0;
}
mFirebaseRemoteConfig.fetch(cacheExpiration)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Vernieuwd",
Toast.LENGTH_SHORT).show();
mFirebaseRemoteConfig.activateFetched();
} else {
Toast.makeText(MainActivity.this, "Vernieuwen mislukt",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
远程配置默认值:
<?xml version="1.0" encoding="utf-8"?>
<defaultsMap>
<entry>
<key>vernieuwen</key>
<value>true</value>-
</entry>