How to save preferences to somewhere other than SharedPreferences
My application is used on multiple platforms so it saves it preferences to a file (rather than to the standard Android SharedPreferences).
Is there any easy of re开发者_开发知识库using the PreferenceActivity to save preferences to a file or is it a case of creating a whole new activity to do the job? If the latter is the case is there a layout I can use that will make the activity look like the normal preferences screen? PreferenceActivity uses com.android.internal.R.layout.preference_list_content but this doesn't appear to be available to apps for reuse.
Is there any easy of reusing the PreferenceActivity to save preferences to a file or is it a case of creating a whole new activity to do the job?
Not really. I mean, you could subclass SharedPreferences
, rip the guts out, and map it to your own data model, but that would be far, far worse for maintainability than just using SharedPreferences
in the first place.
If the latter is the case is there a layout I can use that will make the activity look like the normal preferences screen?
It's just a ListView
. It will take you a lot more time to do this than to just use SharedPreferences
.
PreferenceActivity uses com.android.internal.R.layout.preference_list_content but this doesn't appear to be available to apps for reuse.
Sure it is. If you have the SDK installed, it's on your hard drive right now. Look in $ANDROID_HOME/platforms/$API/data/res/layout
, where $ANDROID_HOME
is where you have the SDK and $API
is some API (e.g., android-2.1
).
You can create a function that exports the data from SharedPreferences
to a file.
Normally Preferences are saved into sharedpreferences when the user clicks on the prefrence item. Add your code to update your external dataholder on onPreferenceTreeClick() function.
精彩评论