Android PackageInfo类中提供了两个变量firstInstallTime和lastUpdateTime,其中firstInstallTime表示App首次安装的时间戳,lastUpdateTime表示App上次更新的时间,当用户首次安装App的时候,这两个值会相等。并且这两个时Android系统来维护的,清除数据不会清除firstInstallTime, lastUpdateTime的值。
public boolean checkIsFirstInstall() {
PackageManager packageManager = getApplication().getPackageManager();
try {
PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
return packageInfo.firstInstallTime == packageInfo.lastUpdateTime;
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException("Failed to get package info", e);
}
}
网友评论