Clearing App Data on update

こ雲淡風輕ζ 提交于 2019-12-06 05:45:45

问题


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.

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).

Any Suggestions?


回答1:


You can store versionCode in SharedPreferences and compare with current versionCode

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();
        }
    }

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




回答2:


You can simply use a PACKAGE_REPLACED receiver which gets fired whenever a package is replaced in your phone (also happens when updating an app). Declare it in your manifest:

<receiver android:name=".UpdateReciever">
    <intent-filter>
        <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
        <data android:scheme="package" android:path="com.my.app" />
    </intent-filter>
</receiver>

And add your receiver class. onReceive will get called when the app is updated:

public class UpdateReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context con, Intent intent) {

    }
}

EDIT: you don't need to filter the package. Use MY_PACKAGE_REPLACED instead which will fire only for your app.




回答3:


You can listen to a system broadcast with following intent and filter in onReceive with your package name to see if your app is updated.

<intent-filter>
   <action android:name="android.intent.action.PACKAGE_REPLACED" />
   <data android:scheme="package" />
</intent-filter>



回答4:


If you are using Android Studio, then you can get vesrion name and vesrion code as BuildConfig.VERSION_CODE for that you should specify in gradle file -

defaultConfig {
    versionCode 1    
    versionName "1.0.0"
}


来源:https://stackoverflow.com/questions/48520051/clearing-app-data-on-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!