Implementing 'profiles' that load/save sets of preferences?
I'm working on a live wallpaper that is configured using a preferences screen. I use shared preferences to store things like color, speed and movement of the wallpaper. As I've got a lot of settings, I'd like users to be able to:
- save all the current preferences under a profile, where the profile is assign a name by the user.
- the user can then select a profile by name from a list and have all their settings restored.
- the user开发者_JAVA技巧 can also delete profiles.
Can anyone recommend a nice way of doing this?
One idea I had was to save all the current preferences to an XML file and selecting a profile would just load the file and set the shared preferences based on this. However, if I had 20 or so profiles, I would need to inspect all the files to produce a list of profile names (as I'd have to store the profile name in each file) which seem inefficient.
I implemented a profile system in one of my apps with the help of an SQLiteDatabase
(basics are explained in the notepad tutorial). You only need one table to store profile name/id, and all of the settings you want.
- save each profile as a new record in your table, with an appropriate name field
- retrieve all records and display the list of names when you want to user to be able to select a profile (e.g.
SELECT * FROM Profile
) - delete a profile in your system by deleting the corresponding record (e.g.
DELETE FROM Profile WHERE name='john'
)
I also saved the name/identifier of the current profile using SharedPreferences
so that my system had an easy way to know which profile was currently/most recently active, outside of the context of profile loading.
精彩评论