如何仅针对某些应用程序版本显示 ChangeLog Alert?
How to show ChangeLog Alert only for certain app versions?
我需要在应用程序启动时显示更新日志,我会在其中向用户说明上次更新消息,当用户关闭该视图时,我必须保存有关它的信息,这样我才能防止它再次打开(如果应用程序的新版本不再需要它,因为并非所有版本都可能需要显示更新日志)。
所以在我的MainActivity
中调用这个函数
private void checkFirstRun(SharedPreferences sharedPreferences) {
final String PREFS_NAME = "FirstRun";
final String PREF_CHANGELOG_ALERT = "changelog";
final String PREF_VERSION_CODE_KEY = "version_code";
final int DOESNT_EXIST = -1;
int currentVersionCode = BuildConfig.VERSION_CODE;
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
int savedVersionCode = prefs.getInt(PREF_VERSION_CODE_KEY, DOESNT_EXIST);
boolean firstTimeChangelog = prefs.getBoolean(PREF_CHANGELOG_ALERT, true);
if (savedVersionCode < 65) {
// If the App version is lower than 65 (old shared prefs) i need to migrate them.
migratePreferences(sharedPreferences);
}
if (currentVersionCode == 65 && firstTimeChangelog) {
// If the App version is 65 (new features and layouts has been added) i need to show a change log activity which informs the user about all the news, but i need to show it only once.
Intent intent = new Intent(MainActivity.this, ChangelogSlider.class);
startActivity(intent);
}
prefs.edit().putInt(PREF_VERSION_CODE_KEY, currentVersionCode).apply();
}
然后在我的 ChangelogSlider.class
中,一旦用户按下“知道了”,我将 PREF_CHANGELOG_ALERT
设置为 false
。
但是如果像版本 69 一样,我需要再次显示该警报,我应该如何表现?
根据@Vucko 的建议
我已将代码更改如下:
private void checkFirstRun(SharedPreferences sharedPreferences) {
final String PREFS_NAME = "FirstRun";
final String PREF_CHANGELOG_ALERT = "changelog";
final String PREF_VERSION_CODE_KEY = "version_code";
final int DOESNT_EXIST = -1;
final int VERSION_WITH_CHANGELOG = 65;
int currentVersionCode = BuildConfig.VERSION_CODE;
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
int savedVersionCode = prefs.getInt(PREF_VERSION_CODE_KEY, DOESNT_EXIST);
int lastChangelogVersion = prefs.getInt(PREF_CHANGELOG_ALERT, VERSION_WITH_CHANGELOG);
if (savedVersionCode < 65) {
migratePreferences(sharedPreferences);
}
if (savedVersionCode < lastChangelogVersion) {
Intent intent = new Intent(MainActivity.this, ChangelogSlider.class);
startActivity(intent);
}
prefs.edit().putInt(PREF_VERSION_CODE_KEY, currentVersionCode).apply();
}
因此,如果我将 VERSION_WITH_CHANGELOG
设置为比当前版本更高的版本,我将显示 ChangeLog,否则如果在新版本中我不需要它,我将保留旧值。
我需要在应用程序启动时显示更新日志,我会在其中向用户说明上次更新消息,当用户关闭该视图时,我必须保存有关它的信息,这样我才能防止它再次打开(如果应用程序的新版本不再需要它,因为并非所有版本都可能需要显示更新日志)。
所以在我的MainActivity
private void checkFirstRun(SharedPreferences sharedPreferences) {
final String PREFS_NAME = "FirstRun";
final String PREF_CHANGELOG_ALERT = "changelog";
final String PREF_VERSION_CODE_KEY = "version_code";
final int DOESNT_EXIST = -1;
int currentVersionCode = BuildConfig.VERSION_CODE;
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
int savedVersionCode = prefs.getInt(PREF_VERSION_CODE_KEY, DOESNT_EXIST);
boolean firstTimeChangelog = prefs.getBoolean(PREF_CHANGELOG_ALERT, true);
if (savedVersionCode < 65) {
// If the App version is lower than 65 (old shared prefs) i need to migrate them.
migratePreferences(sharedPreferences);
}
if (currentVersionCode == 65 && firstTimeChangelog) {
// If the App version is 65 (new features and layouts has been added) i need to show a change log activity which informs the user about all the news, but i need to show it only once.
Intent intent = new Intent(MainActivity.this, ChangelogSlider.class);
startActivity(intent);
}
prefs.edit().putInt(PREF_VERSION_CODE_KEY, currentVersionCode).apply();
}
然后在我的 ChangelogSlider.class
中,一旦用户按下“知道了”,我将 PREF_CHANGELOG_ALERT
设置为 false
。
但是如果像版本 69 一样,我需要再次显示该警报,我应该如何表现?
根据@Vucko 的建议
我已将代码更改如下:
private void checkFirstRun(SharedPreferences sharedPreferences) {
final String PREFS_NAME = "FirstRun";
final String PREF_CHANGELOG_ALERT = "changelog";
final String PREF_VERSION_CODE_KEY = "version_code";
final int DOESNT_EXIST = -1;
final int VERSION_WITH_CHANGELOG = 65;
int currentVersionCode = BuildConfig.VERSION_CODE;
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
int savedVersionCode = prefs.getInt(PREF_VERSION_CODE_KEY, DOESNT_EXIST);
int lastChangelogVersion = prefs.getInt(PREF_CHANGELOG_ALERT, VERSION_WITH_CHANGELOG);
if (savedVersionCode < 65) {
migratePreferences(sharedPreferences);
}
if (savedVersionCode < lastChangelogVersion) {
Intent intent = new Intent(MainActivity.this, ChangelogSlider.class);
startActivity(intent);
}
prefs.edit().putInt(PREF_VERSION_CODE_KEY, currentVersionCode).apply();
}
因此,如果我将 VERSION_WITH_CHANGELOG
设置为比当前版本更高的版本,我将显示 ChangeLog,否则如果在新版本中我不需要它,我将保留旧值。