Personlink Advent Calendar, 17th day post: clap: Today, I will write about the version of the Android app. I look forward to working with you.
Both Java and Android Studio are beginners, so if you have any mistakes, I would appreciate it if you could comment. Here is the version you are currently using. Android Studio : 3.4.2
You may see version information such as "1.3.2" in apps and software. In Android Studio, this is called Version Name and can be edited by the creator. Typically 1st digit: Major version Second digit: Minor version 3rd digit: minor minor version Is called.
On the other hand, Version Code increments the number of updates. You will need it to simply see the number of updates.
Version Name is just a name, so I don't think it is recommended, but you can change it even if you update it.
This information is described in build.gradle. If you rewrite the version code of build.gradle, it will automatically update the places where other rewriting processing is required. It will be saved
Judges the first launch that the app was downloaded and opened for the first time. SharedPreferences manages setting information such as the version of the app. To write data, edit it with editor and save it with apply or commit. Use get to get the data. (Search for Shared Preferences around here)
first.java
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
//Settings related class
public class PreferencesUtil {
//Set value identifier
private enum Key {
IS_FIRST
}
//Judgment whether it is the first start
public static boolean isFirstJudgment(Context context) {
return getSharedPreferences(context).getBoolean(Key.IS_FIRST.name(), true);
}
//Setting the first start flag
public static void setFirstFlag(Context context, boolean isFirst) {
Editor edit = getSharedPreferences(context).edit();
edit.putBoolean(Key.IS_FIRST.name(), isFirst);
edit.apply();
}
}
Call the isFirstJudgment method when you want to determine whether it is the first start. In this case, if true, it means that it is the first startup.
By setting true-> false after the first startup, it will be judged that the next startup is not the first startup. In that case, set it to false as follows.
PreferencesUtil.setApplicationFirst(getApplicationContext(), false);
Triggered by the increment of Version Code, it judges the first startup after the update.
update.java
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
//Settings related class
public class PreferencesUtil {
public static boolean isLastVersion(Context context) {
//Get the current version
int currentCode = getSharedPreferences(context).getInt("VersionCode", 1);
//Get the latest version
int latestCode = ApplicationUtil.getVersionCode(context);
//Compare current and latest version
if (latestCode > currentCode) {
return true;
} else {
return false;
}
}
}
The currentCode will be the Version Code in the last saved device. The latest Code is getting the latest Version Code downloaded. By comparing these two variables, it is judged whether it is the latest version.
In this case, if true, it means that it will be started for the first time after updating. After that, by introducing the value of latestCode to currentCode, false will be returned after that.
These processes can be used only once to set up push notifications, pop up a privacy policy agreement, or notify you of updates!
That's it for today's article. Tomorrow is @kuwakuwakuwa. I look forward to working with you!
Recommended Posts