How can you tell if it's the first time the app starts?
It would be interesting, in the case that the user updates the application is detected as new or update.
How can you tell if it's the first time the app starts?
It would be interesting, in the case that the user updates the application is detected as new or update.
You can use Shared Preferences to save values persistently. Then you could save a variable that contains the name of the app version and check it every time you start the application. In addition, the Shared Preferences are maintained even after updating, so this method would also serve to detect the first execution after an update.
The idea would be that you have a constant with the version of the application (for example, a String of type "1.0.0"). And then follow an algorithm like this when starting the application:
If the value of the constant is different from the value of Shared Preferences from step 1, then it is the first time the app is started:
Save the value of the constant with the version in the Shared Preferences .
Solved , taking ideas from the answers:
private int getFirstTimeRun() {
SharedPreferences sp = getSharedPreferences("MYAPP", 0);
int result, currentVersionCode = BuildConfig.VERSION_CODE;
int lastVersionCode = sp.getInt("FIRSTTIMERUN", -1);
if (lastVersionCode == -1) result = 0; else
result = (lastVersionCode == currentVersionCode) ? 1 : 2;
sp.edit().putInt("FIRSTTIMERUN", currentVersionCode).apply();
return result;
}
The values returned by the function:
Example of use:
switch(getFirstTimeRun()) {
case 0:
Log.d("appPreferences", "Es la primera vez!");
break;
case 1:
Log.d("appPreferences", "ya has iniciado la app alguna vez");
break;
case 2:
Log.d("appPreferences", "es una versión nueva");
}
It accepts improvements in functionality and code optimization.
EDITED
Another way is to use the library Once :
Initialize with: Once.initialise(this);
First time methods:
The first time the user installs the app:
if (!Once.beenDone(Once.THIS_APP_INSTALL, "tag")) {
//Primera vez que se instala la app
Once.markDone("tag");
}
The first time after the app's upgrade:
if (!Once.beenDone(Once.THIS_APP_VERSION, "tag")) {
//Primera vez despues de actualizar la app
Once.markDone("tag");
}
UPDATED APRIL 2017
There is also the possibility to obtain if it is a 0 installation or an update, obtaining the APK values
public static boolean isFirstInstall(Context context) {
try {
long firstInstallTime = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).firstInstallTime;
long lastUpdateTime = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).lastUpdateTime;
return firstInstallTime == lastUpdateTime;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return false;
}
}
public static boolean isInstallFromUpdate(Context context) {
try {
long firstInstallTime = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).firstInstallTime;
long lastUpdateTime = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).lastUpdateTime;
return firstInstallTime != lastUpdateTime;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return false;
}
You can use SharedPreferences , there are Take into account that these values can be lost when you uninstall the application.
You can save using the setInicia()
method, sending the value of 1 when it is the first installation and you can obtain the value of that preference using the getInicia()
public static int getInicia(Context ctx){
response = ctx.getSharedPreferences("INSTALACION", 0)
.getInt("primeravez", 0);
return response;
}
public static void setInicia(Context ctx, int mode){
ctx.getSharedPreferences("INSTALACION", 0).edit()
.putInt("primeravez", mode).commit();
}
However if you want to define the first time you start the application on a device, the method (although it is also not completely infallible) that I use is to write a file containing the string "1", inside the directory /Android/data
and not within the application directory /Android/data/com.midominio.miapp
File installation = new File(getExternalInstallPath(context));
try {
if (!installation.exists()){
createInstallDirectory(context);
writeInstallationFile(installation);
}
sID = readInstallationFile(installation);
Log.i(TAG, "el status de la instalación es: " + sID);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
These are the methods used:
private static void writeInstallationFile(File installation) throws IOException {
FileOutputStream out = new FileOutputStream(installation, false);
String id = "1";
Log.i(TAG, "Writing Installation File: " + id);
out.write(id.getBytes());
out.close();
}
private static String readInstallationFile(File installation) throws IOException {
RandomAccessFile f = new RandomAccessFile(installation, "r");
byte[] bytes = new byte[(int) f.length()];
f.readFully(bytes);
f.close();
return new String(bytes);
}
public static File createInstallDirectory(Context ctx) throws IOException {
String directoryPath=getExternalInstallPath(ctx);
File dir = new File(directoryPath);
if (dir.exists()) {
return dir;
}
if (dir.mkdirs()) {
return dir;
}
throw new IOException("Failed to create INSTALL '" + directoryPath + "' for an unknown reason.");
}
public static String getExternalInstallPath(Context ctx){
String directoryPath=Environment.getExternalStorageDirectory().getPath();
directoryPath = directoryPath + "/Android/data/");
return directoryPath;
}