Skip to content

Commit 20290bb

Browse files
Added 2 very useful methods
1. isFirstTimeInstall(): If you want to know whether this is the first time installation of this app in this device. 2. isAppUpgraded(): If you want to know whether the current version was installed over a previous version (update/upgrade) or if it is freshly installed (clean install). Code: /** * Return true if this is the first ever time that the application is installed on the device. * * @return true if this is the first ever time that the application is installed on the device. */ public static boolean isFirstTimeInstall(){ try { Long firstInstallTime = Utils.getApp().getPackageManager().getPackageInfo(this.getAppPackageName(), 0).firstInstallTime; Long lastUpdateTime = Utils.getApp().getPackageManager().getPackageInfo(this.getAppPackageName(), 0).lastUpdateTime; return firstInstallTime == lastUpdateTime; } catch (Exception e) { return false; } } /** * Return true if app was previously installed and this one is an update/upgrade to that one, returns false if this is a fresh installation and not an update/upgrade. * * @return true if app was previously installed and this one is an update/upgrade to that one, returns false if this is a fresh installation and not an update/upgrade. */ public static boolean isAppUpgraded(){ try { Long firstInstallTime = Utils.getApp().getPackageManager().getPackageInfo(this.getAppPackageName(), 0).firstInstallTime; Long lastUpdateTime = Utils.getApp().getPackageManager().getPackageInfo(this.getAppPackageName(), 0).lastUpdateTime; return firstInstallTime != lastUpdateTime; } catch (Exception e) { return false; } }
1 parent 66a4c04 commit 20290bb

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

lib/utilcode/src/main/java/com/blankj/utilcode/util/AppUtils.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,38 @@ public static int getAppIconId(final String packageName) {
384384
}
385385
}
386386

387+
388+
/**
389+
* Return true if this is the first ever time that the application is installed on the device.
390+
*
391+
* @return true if this is the first ever time that the application is installed on the device.
392+
*/
393+
public static boolean isFirstTimeInstall(){
394+
try {
395+
Long firstInstallTime = Utils.getApp().getPackageManager().getPackageInfo(this.getAppPackageName(), 0).firstInstallTime;
396+
Long lastUpdateTime = Utils.getApp().getPackageManager().getPackageInfo(this.getAppPackageName(), 0).lastUpdateTime;
397+
return firstInstallTime == lastUpdateTime;
398+
} catch (Exception e) {
399+
return false;
400+
}
401+
}
402+
403+
/**
404+
* Return true if app was previously installed and this one is an update/upgrade to that one, returns false if this is a fresh installation and not an update/upgrade.
405+
*
406+
* @return true if app was previously installed and this one is an update/upgrade to that one, returns false if this is a fresh installation and not an update/upgrade.
407+
*/
408+
public static boolean isAppUpgraded(){
409+
try {
410+
Long firstInstallTime = Utils.getApp().getPackageManager().getPackageInfo(this.getAppPackageName(), 0).firstInstallTime;
411+
Long lastUpdateTime = Utils.getApp().getPackageManager().getPackageInfo(this.getAppPackageName(), 0).lastUpdateTime;
412+
return firstInstallTime != lastUpdateTime;
413+
} catch (Exception e) {
414+
return false;
415+
}
416+
}
417+
418+
387419
/**
388420
* Return the application's package name.
389421
*

0 commit comments

Comments
 (0)