当我的应用在Android Play商店更新时,我想清除缓存?

6

我有一些数据存储在偏好设置中。如果在PlayStore中发现任何更新,我需要监听更新操作并清除我的应用程序的缓存。


请参考以下链接,了解如何在Android应用程序中编程清除缓存:https://dev59.com/_WAg5IYBdhLWcg3wLITS - sasikumar
1
我知道如何清除缓存。我想要检查新更新版本的第一次启动... - Raja Jawahar
更简单的方法:https://stackoverflow.com/a/76526011/1397821 - M. Usman Khan
2个回答

2

如何检查您的应用程序是否已更新:

将当前版本代码存储在共享首选项中,然后在主活动中使用以下函数。

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://dev59.com/_WAg5IYBdhLWcg3wLITS#23908638


非常感谢您的回复。我对此没问题。但是我想知道,这个有没有单独的动作监听器? - Raja Jawahar
1
有一个动作,我猜是ACTION_PACKAGE_REPLACED。你需要实现一个接收器来监听这个动作。在我看来,存储版本代码更简单。 - Yash
我知道这个更简单。我想应该还有其他的选择.. 不管怎样,非常感谢。 - Raja Jawahar

0

我们必须内部处理。以下是我以简单方式处理它的方法。

对于任何版本,如果需要清除数据,我会在gradle文件中创建一个带有应用程序versionCode的字段来进行数据清除:

defaultConfig {
        minSdkVersion 24
        targetSdkVersion 33
        buildConfigField("int", "CLEAR_DATA_VER", "123")
    }

在我的第一个活动中,我检查数据是否已清除为该版本。
    ...

    val dataClearedVersion = Constants.dataClearedVersion(context)

    if (dataClearedVersion == null || dataClearedVersion < BuildConfig.CLEAR_DATA_VER ){
       Constants.cleanSharedPreferences(context)
       Constants.setDataClearedVersion(context)
     }
...

 fun dataClearedVersion(context: Context): Int? {
        val sharedPref = context.applicationContext.getSharedPreferences(PREF_KEY, Context.MODE_PRIVATE)
        val version = sharedPref.getInt(APP_DATA_CLEARED_K, -1)

        return if (version == -1) null else version
    }


fun setDataClearedVersion(context: Context) {
    val sharedPref = context.applicationContext.getSharedPreferences(PREF_KEY, Context.MODE_PRIVATE).edit()
    sharedPref.putInt(APP_DATA_CLEARED_K, BuildConfig.CLEAR_DATA_VER).apply()
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接