Blankj / Blankj/AndroidUtilCode

Detect if app is newly installed or updated

オープン
#1,430 コメント 0 件 リアクション 0 件 担当者 1 名 @Blankj が担当を希望しています GitHub で見る
help wanted
主要言語
Java
スター
33.6k
フォーク
10.6k
PR マージ指標
30日以内にマージされた PR はありません

説明

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!

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。