简体   繁体   English

在Android中重新安装应用程序后检索设置

[英]Retrieve settings after app reinstall in android

I have an app which saves some settings using PreferenceManager. 我有一个使用PreferenceManager保存一些设置的应用程序。 Later I uninstall the application and later want to retrieve the same settings back when I reinstall the same application. 后来我卸载了该应用程序,后来又想在重新安装相同的应用程序时取回相同的设置。 For eg: I have a checkbox whose state is saved using preference manager and later uninstall the app. 例如:我有一个复选框,其状态使用首选项管理器保存,然后卸载应用程序。 Later when I reinstall the app I need to get the state back of that checkbox. 稍后,当我重新安装该应用程序时,我需要获取该复选框的状态。 Is this possible to do? 这可能吗? If not with preferenceManager is there any other way to do it ie by saving in DB? 如果没有使用preferenceManager,还有其他方法可以做到,即通过保存在DB中吗?

SharedPreference and Database won't be kept after uninstalling an app. 卸载应用程序后,将不会保留SharedPreference和Database。
That being said, Android provides an easy way to send your SharedPreferences to the cloud and retrieve them when re-installing an app. 话虽如此,Android提供了一种简便的方法,可将您的SharedPreferences发送到云中并在重新安装应用程序时检索它们。 This mechanism is called BackupAgentHelper . 此机制称为BackupAgentHelper

You can keep a backup file for shared preferences in the external storage and when you install the application again you can update the shared preferences from the backup file. 您可以将共享首选项的备份文件保留在外部存储中,当再次安装该应用程序时,可以从备份文件更新共享首选项。 Or you can use BackupAgentHelper/SharedPreferenceBackupHelper to store the data on cloud. 或者,您可以使用BackupAgentHelper/SharedPreferenceBackupHelper将数据存储在云上。 For the SQLite however, if you keep the database files in a different directory those will not be removed by Android when uninstalling the application. 但是对于SQLite ,如果将数据库文件保存在其他目录中,则在卸载应用程序时,Android不会将其删除。 In case of a SQLite database you may set the database directory in the SQLiteOpenHelper constructor. 如果是SQLite数据库,则可以在SQLiteOpenHelper构造函数中设置数据库目录。 Lets say you want the path set as: 假设您要将路径设置为:

final static String DB_PATH = Environment.getExternalStorageDirectory().getAbsolutePath().concat("/databases/my_db.db");

Then in the sqlite helper you set it as follows: 然后在sqlite助手中按如下所示进行设置:

public DbHelper(Context context, String name, CursorFactory factory,
        int version) {
    super(context, DB_PATH, factory, version);
}

Meanwhile in case of the SharedPreferences you may copy the file from the default app data directory to a folder of your chosen in the sd card. 同时,如果使用SharedPreferences,则可以将文件从默认应用程序数据目录复制到sd卡中所选的文件夹中。 You need to pay attention here in order to choose the appropriate moment when to backup the SharedPreferences file. 您需要在这里注意以便选择适当的时间来备份SharedPreferences文件。 As a IO operation it might require time to complete. 作为IO操作,可能需要一些时间才能完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM