更新时清除应用程序数据 [英] Clearing App Data on update

查看:133
本文介绍了更新时清除应用程序数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在用户更新时以编程方式清除(等同于应用程序设置中的清除数据)应用程序中的所有旧数据。 Google Play商店或任何其他来源的应用程序。这是因为与旧软件相比,我已经更改了新应用程序中的所有内容,因此我不需要旧应用程序中的任何现有数据。

I need to clear(equivalent to Clear Data in App Settings) all the old data in the app programmatically when the user updates the app from Google Play Store or any other sources. This is because I don't need any of the existing data from the old app since I have changed everything in the new app compared to the old one.

我想到了在应用启动时实施版本检查,但是我找不到一种方法来获取应用的上一个 versionCode versionName 。我发现清除数据的唯一方法是在发布应用程序时检查 lastUpdateTime 。但这是不可靠的,因为用户还有其他方式或来源来获取该应用程序(例如与朋友共享该应用程序,或者如果用户拥有旧应用程序的备份)。

I thought of implementing a version check at the app startup, but I can't find a way to get the app's previous versionCode or versionName. The only way I figured out to clear the data is to check the lastUpdateTime at the time of publishing the app. But it's not reliable since the user has other ways or sources of getting the app (like sharing it with a friend or if the user had a backup of the old app).

有任何建议吗?

推荐答案

您可以将 versionCode 存储在<$ c中$ c> SharedPreferences 并与当前 versionCode

You can store versionCode in SharedPreferences and compare with current versionCode

进行比较需要清除SharedPreferences,本地数据库和您存储的所有本地文件的所有值。

For clear all data you just need to clear all value of SharedPreferences, Local Database and all local files which you have store.

public void clearData()
    {
        try {
            PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            int mCurrentVersion = pInfo.versionCode;
            SharedPreferences mSharedPreferences = getSharedPreferences("app_name",  Context.MODE_PRIVATE);
            SharedPreferences.Editor mEditor = mSharedPreferences.edit();
            mEditor.apply();
            int last_version = mSharedPreferences.getInt("last_version", -1);
            if(last_version != mCurrentVersion)
            {
                //clear all your data like database, share preference, local file 
                //Note : Don't delete last_version value in share preference
            }
            mEditor.putInt("last_version", mCurrentVersion);
            mEditor.commit();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

注意:不要t删除共享首选项中的 last_version 值。

Note : Don't delete last_version value in share preference.

这篇关于更新时清除应用程序数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆