Android file storage-use SharedPreferences to save user preference setting parameters and read setting parameters
What file storage technologies does Android SDK support?
1. Use SharedPreferences to save key-value data
2. Stream file storage (using the openFileOutput and openFileInput methods, or FileInputStream and FileOutputStream)
3. XML semi-structured storage
4. Save arrays and objects in JSON format
5. Use a database to save structured data
6. Use a third-party object-oriented database to directly save Java objects.
This blog post describes how to use SharedPreferences to save the key-value Pair and how to read and set parameters.
1. Use the Context. getSharedPreferences method to obtain the SharedPreferences object. The name of the file storing the key-value is specified by the first parameter of the getSharedPreferences method.
2. Use the SharedPreference. edit method to obtain the SharedPreferences. Editor object.
3. Use the putXxx method of the SharedPreference. Editor interface to save the key-value pair.
4. Submit the key-value pair to be saved using the SharedPreference. Editor. commit method.
Instance: SharedPreferences
MainActivity. java
[Java]
Package com. wwj. setting;
Import java. util. Map;
Import com. wwj. service. PreferencesService;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. EditText;
Import android. widget. RadioGroup;
Import android. widget. Toast;
Public class MainActivity extends Activity {
Private EditText nameText; // Name box
Private EditText ageText; // age box
Private RadioGroup radioGroup; // single-region Group
// Business logic
Private PreferencesService service;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
NameText = (EditText) findViewById (R. id. nameText );
AgeText = (EditText) findViewById (R. id. ageText );
RadioGroup = (RadioGroup) findViewById (R. id. radioGroup );
Service = new PreferencesService (this );
Map <String, String> params = service. getPerferences ();
NameText. setText (params. get ("name "));
AgeText. setText (params. get ("age "));
RadioGroup. check (Integer. valueOf (params. get ("sex"); // set the selected radio button
}
/**
* The onClick method specified by the button control in the layout
* @ Param v
*/
Public void save (View v ){
String name = nameText. getText (). toString ();
String age = ageText. getText (). toString ();
Integer sex = radioGroup. getCheckedRadioButtonId ();
Service. save (name, Integer. valueOf (age), sex );
Toast. makeText (getApplicationContext (), R. string. success, 1). show ();
}
}
Package com. wwj. setting;
Import java. util. Map;
Import com. wwj. service. PreferencesService;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. EditText;
Import android. widget. RadioGroup;
Import android. widget. Toast;
Public class MainActivity extends Activity {
Private EditText nameText; // Name box
Private EditText ageText; // age box
Private RadioGroup radioGroup; // single-region Group
// Business logic
Private PreferencesService service;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
NameText = (EditText) findViewById (R. id. nameText );
AgeText = (EditText) findViewById (R. id. ageText );
RadioGroup = (RadioGroup) findViewById (R. id. radioGroup );
Service = new PreferencesService (this );
Map <String, String> params = service. getPerferences ();
NameText. setText (params. get ("name "));
AgeText. setText (params. get ("age "));
RadioGroup. check (Integer. valueOf (params. get ("sex"); // set the selected radio button
}
/**
* The onClick method specified by the button control in the layout
* @ Param v
*/
Public void save (View v ){
String name = nameText. getText (). toString ();
String age = ageText. getText (). toString ();
Integer sex = radioGroup. getCheckedRadioButtonId ();
Service. save (name, Integer. valueOf (age), sex );
Toast. makeText (getApplicationContext (), R. string. success, 1). show ();
}
}
PreferencesService. java
[Java]
Package com. wwj. service;
Import java. util. HashMap;
Import java. util. Map;
Import android. content. Context;
Import android. content. SharedPreferences;
Import android. content. SharedPreferences. Editor;
Public class PreferencesService {
Private Context context;
Public PreferencesService (Context context ){
This. context = context;
}
/**
* Save Parameters
* @ Param name
* @ Param age
* @ Param sex gender
*/
Public void save (String name, Integer age, Integer sex ){
// Obtain the SharedPreferences object
SharedPreferences preferences = context. getSharedPreferences ("wwj", Context. MODE_PRIVATE );
Editor editor = preferences. edit ();
Editor. putString ("name", name );
Editor. putInt ("age", age );
Editor. putInt ("sex", sex );
Editor. commit ();
}
/**
* Obtain parameters
* @ Return
*/
Public Map <String, String> getPerferences (){
Map <String, String> params = new HashMap <String, String> ();
SharedPreferences preferences = context. getSharedPreferences ("wwj", Context. MODE_PRIVATE );
Params. put ("name", preferences. getString ("name ",""));
Params. put ("age", String. valueOf (preferences. getInt ("age", 0 )));
Params. put ("sex", String. valueOf (preferences. getInt ("sex", 0 )));
Return params;
}
}
Package com. wwj. service;
Import java. util. HashMap;
Import java. util. Map;
Import android. content. Context;
Import android. content. SharedPreferences;
Import android. content. SharedPreferences. Editor;
Public class PreferencesService {
Private Context context;
Public PreferencesService (Context context ){
This. context = context;
}
/**
* Save Parameters
* @ Param name
* @ Param age
* @ Param sex gender
*/
Public void save (String name, Integer age, Integer sex ){
// Obtain the SharedPreferences object
SharedPreferences preferences = context. getSharedPreferences ("wwj", Context. MODE_PRIVATE );
Editor editor = preferences. edit ();
Editor. putString ("name", name );
Editor. putInt ("age", age );
Editor. putInt ("sex", sex );
Editor. commit ();
}
/**
* Obtain parameters
* @ Return
*/
Public Map <String, String> getPerferences (){
Map <String, String> params = new HashMap <String, String> ();
SharedPreferences preferences = context. getSharedPreferences ("wwj", Context. MODE_PRIVATE );
Params. put ("name", preferences. getString ("name ",""));
Params. put ("age", String. valueOf (preferences. getInt ("age", 0 )));
Params. put ("sex", String. valueOf (preferences. getInt ("sex", 0 )));
Return params;
}
}
Layout file activity_main.xml
[Html]
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: id = "@ + id/LinearLayout1"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/name"/>
<EditText
Android: id = "@ + id/nameText"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
/>
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/age"/>
<EditText
Android: id = "@ + id/ageText"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: numeric = "integer"
/>
<RadioGroup
Android: id = "@ + id/radioGroup"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: contentDescription = "gender">
<RadioButton
Android: id = "@ + id/radioButton1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/male"
Android: checked = "true"/>
<RadioButton
Android: id = "@ + id/radioButton2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/female"/>
</RadioGroup>
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: onClick = "save"
Android: text = "@ string/saveBtn"/>
</LinearLayout>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: id = "@ + id/LinearLayout1"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/name"/>
<EditText
Android: id = "@ + id/nameText"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
/>
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/age"/>
<EditText
Android: id = "@ + id/ageText"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: numeric = "integer"
/>
<RadioGroup
Android: id = "@ + id/radioGroup"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: contentDescription = "gender">
<RadioButton
Android: id = "@ + id/radioButton1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/male"
Android: checked = "true"/>
<RadioButton
Android: id = "@ + id/radioButton2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/female"/>
</RadioGroup>
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: onClick = "save"
Android: text = "@ string/saveBtn"/>
</LinearLayout>