Blankj / Blankj/AndroidUtilCode

Detect if app is newly installed or updated

Open
#1,430 0 comments 0 reactions 1 assignee Claimed by @Blankj View on GitHub
help wanted
Dominant language
Java
Stars
33.6k
Forks
10.6k
PR merge metrics
No merged PRs in 30d

Description

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!

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.