In android is there any way to preserve SharedPreferences after an uninstall

人走茶凉 提交于 2019-12-17 09:33:35

问题


I am keeping some application meta data in SharedPreferences. Whenever I uninstall the application and reinstall it, the SharedPreferences are deleted.

Is there any way to get that to remain, so that if the user does an uninstall and reinstall, they can recover their old data?


回答1:


You should add a BackupAgentHelper to your app. Together with the SharedPreferenceBackupHelper, it backups the SharedPreferences to the cloud (if the device supports it). When the app is reinstalled the data is restored.

See:

BackupAgentHelper

SharedPreferenceHelper (contains all the code you need to implement it)

general Backup guide




回答2:


Since Android 6.0 it has been possible to use:

<application
        android:allowBackup="true">

By setting it to true your data (sharedprefs and others) will be saved on Google cloud and restored next time the app is installed. You can read more about it here. It should be noted that the default settings is true since 6.0.




回答3:


I'm pretty sure SharedPreferences is always deleted along with the app. In my opinion, the best way to do this would be to write a hidden file (something like ".nameOfFile") onto the SD Card or internal memory and have that contain the preferences as well.

You should use SharedPreferences though, it's the Android standard for preference management. You could make it so that the first time your app loads, it checks the SDCard for a hidden file that would have been created last time they opened it. If the file exists, then read in those inputs and store them in the SharedPreferences, if it doesn't, then either the user deleted it or the user has never installed your app before.

This is just one way to do it, and it might not be the most efficient, but I hope it helps!




回答4:


One way to do this would be to store the user data on a server. Then when the user re-installs the app or installs the app on another device, they can "sync" their user data. That would just be a small HTTP download of the data, likely stored in JSON, which you would then parse and write into the SharedPreferences.

If you don't want to maintain your own server, you could use a cloud service like Dropbox. This is how the app 1Password Reader works.




回答5:


As edthethird said: best lay physical files on device external storage and read them on installation. If the file content needs to be hidden from users just perform a simple encryption/decryption process.




回答6:


sharedPrefs and DBs are removed when you uninstall. You would have to write outside of the app (sd for example).




回答7:


This is actually built-in, you just need to implement a couple of classes to enable it. Data will be backed up and linked to the user's Google account, so it will be automatically restored if they install the app on a new device, re-install, etc.

http://developer.android.com/guide/topics/data/backup.html



来源:https://stackoverflow.com/questions/9815363/in-android-is-there-any-way-to-preserve-sharedpreferences-after-an-uninstall

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