Blankj / Blankj/AndroidUtilCode

Detect if app is newly installed or updated

Aperta
#1,430 0 commenti 0 reazioni 1 assegnatario Rivendicata da @Blankj Vedi su GitHub
help wanted
Lingua principale
Java
Stelle
33.6k
Fork
10.6k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

These 2 features are important and useful to have in AppUtils.
it is to detect whether the current app is installed newly, or got upgraded from a lower version.
There are many use cases for this functionality.

Source:
[https://stackoverflow.com/questions/26352881/detect-if-new-install-or-updated-version-android-app/43570229]

Check if first install:

```
public static boolean isFirstTimeInstalled(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 true;
}
}
```

Check if app is upgraded:

```

public static boolean isAppUpgraded(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;
}
}

```

Thank you and good luck!

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.