gpt4 book ai didi

android - 当我的应用程序在 Android 的 PlayStore 中更新时,我想清除缓存吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:15:45 25 4
gpt4 key购买 nike

我有一些数据存储在首选项中。如果在 PlayStore 中发现任何更新。我需要收听更新操作并且必须清除我的应用程序的缓存。

最佳答案

了解您的应用是否已更新:

在共享首选项中存储当前版本代码,然后在主 Activity 中使用以下功能。

public static AppStart checkAppStart(Context context, SharedPreferences sharedPreferences) {
PackageInfo pInfo;
AppStart appStart = AppStart.NORMAL;
try {
pInfo = context.getPackageManager().getPackageInfo(
context.getPackageName(), PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
int lastVersionCode = sharedPreferences.getInt(
Constants.LAST_APP_VERSION, -1);

int currentVersionCode = pInfo.versionCode;
appStart = checkAppStart(currentVersionCode, lastVersionCode);

// Update version in preferences
sharedPreferences.edit()
.putInt(Constants.LAST_APP_VERSION, currentVersionCode).commit(); // must use commit here or app may not update prefs in time and app will loop into walkthrough
} catch (PackageManager.NameNotFoundException e) {
Log.w(LOG_TAG,
"Unable to determine current app version from package manager. Defensively assuming normal app start.");
}
return appStart;
}

private static AppStart checkAppStart(int currentVersionCode, int lastVersionCode) {
if (lastVersionCode == -1) {
return AppStart.FIRST_TIME;
} else if (lastVersionCode < currentVersionCode) {
return AppStart.FIRST_TIME_VERSION;
} else if (lastVersionCode > currentVersionCode) {
Log.w(LOG_TAG, "Current version code (" + currentVersionCode
+ ") is less then the one recognized on last startup ("
+ lastVersionCode
+ "). Defensively assuming normal app start.");
return AppStart.NORMAL;
} else {
return AppStart.NORMAL;
}
}

AppStart 在这里只是一个枚举:

public enum AppStart {
FIRST_TIME,
FIRST_TIME_VERSION,
NORMAL
}

之后,清除缓存: https://stackoverflow.com/a/23908638/1594776

关于android - 当我的应用程序在 Android 的 PlayStore 中更新时,我想清除缓存吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35547444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com