Suppose you have a relatively small key value for the data you want to save, you should use SharedPreferences APIs. An SharedPreferences object points to a file that includes a key-value pair, which provides an easy way to read and write them. Each SharedPreferences file system framework is managed, they can be private and can be shared.
This lesson will show you how to use sharedpreferences APIs to store and retrieve simple data.
tips: SharedPreferences APIs can only be used to manipulate key-value pairs of type data, not to confuse it with Preference APIs, and preference is used to help users build an app-setting interface. Many other Preference information about APIs, please visit the Settings Wizard.
Gets a reference to the sharedpreferences (handle)
You can create a new shared preference file in any of the following ways or visit a shared preference file that already exists:
getSharedPreferences()-use this if you need multiple GKFX preference files identified by name, which your specify with the first parameter. You can call the this from all in Context your app.
getSharedPreferences()-assuming you have multiple shared preference files saved by different names, you can use this method, the first parameter of this method is the file name. You can use the context object in your app to invoke it.
getPreferences()-assuming you only need to use a shared preference file in this activity, you can invoke this method in the activity. Since this method returns a default shared preference file that belongs to the activity, you do not need to provide a name for it.
For example, the following method will be Fragment run in. It internally interviewed a shared preferences file, which was R.string.preference_file_key specified by this string, and it was opened by private mode, so it could only be visited by your app.
ContextContext=getactivity();sharedpreferencesSharedpref=Context.getsharedpreferences(getString(R.string.Preference_file_key), Context.mode_private);
When naming your shared preference file, you should use a unique identity, such as"com.example.myapp.PREFERENCE_FILE_KEY"
as an alternative, if you just want to use a shared preference file in your activity, you can use getpreferences () method:
sharedpreferences Sharedpref = getactivity (). getpreferences ( context mode_private
Note: If you MODE_WORLD_READABLE MODE_WORLD_WRITEABLE create a shared preference file using or mode, you will be able to access your data regardless of any other app that knows the identity of the file.
Writing data to a shared preferences
To write data to a shared preferences file, you need to call Sharedpreferences's edit () method to create a Sharedpreferences.editor object.
Pass in the keys and values to the method you want to invoke, such as putInt() and putString() . The method is then called commit() to store these changes. Like what:
sharedpreferencesSharedpref=getactivity().getpreferences(Context.mode_private);sharedpreferences.EditorEditor=Sharedpref.Edit();Editor.Putint(getString(R.string.Saved_high_score),Newhighscore);Editor.Commit();
Reading data from the shared preferences
To get data from a shared preferences file, you can call Getint () or GetString (), and so on, you just need to provide the value you want to get the corresponding key, you can also choose to pass in a default value, It is assumed that the default value will be returned by passing the key without a pass-through value. Like what:
sharedpreferencesSharedpref=getactivity().getpreferences(Context.mode_private);intDefaultValue=getresources().Getinteger(R.string.Saved_high_score_default);LongHighscore=Sharedpref.getInt(getString(R.string.Saved_high_score),DefaultValue);
Android Learning Route (27) Key-value pair (sharedpreferences) storage