当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Do some clean-up in preferences file on update

问题描述:

I'd like to change the name of a parameter in my preferences file. The new version of my app expects the new name. If an older version is already installed, it will have created a preferences file that will contain the old parameter name. After updating, the newest version will not be able to find the new name. Therefore I need to change that name.

One obvious solution is for the new version to check the parameter name in the preferences file and change it if necessary. That could be done for example in the onCreate() method. But I would prefer to avoid the burden of doing that check every time the app is started. It would be better if it could be done just once, when the app is updated.

So what I want is to do the clean-up of the preferences as part of the update process. Is it possible to run some Java code as part of the installation? Can the update process do something other than copy the new app files on the old ones? If not, is there any other way to do such changes to the preferences file on update? Note that, although I want to change the name of a parameter in the preferences file, I need to keep its value.

网友答案:

On API12+ you can do your update work in some BroadcastReceiver registered for the Action Intent.ACTION_MY_PACKAGE_REPLACED https://developer.android.com/reference/android/content/Intent.html#ACTION_MY_PACKAGE_REPLACED

If you implemt the update there, you can drop all update calls in some place like Application.onCreate() for API12+. So only old phones nee to check for updates on every start.

分享给朋友:
您可能感兴趣的文章:
随机阅读: