Android uses SharedPreferences to store and read data
Most of the time, the software we develop needs to provide users with the software parameter setting function, such as our commonly used QQ, users can set whether to allow strangers to add themselves as friends. For saving software configuration parameters, if the window software is used, we usually use the INI file for saving. If it is a j2se application, we will use the properties property file or xml for saving. For Android applications, how can we save software configuration parameters? The Android platform provides us with a SharedPreferences class, which is a lightweight storage class and is especially suitable for storing software configuration parameters. SharedPreferences is used to save data. xml files are used to store data in/data/ /Shared_prefs directory.
The following uses the default path settings to illustrate the usage of the layout file:
The following is the PreferencesService class:
Package lhjd. cnc. dispenser. setting; 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 application parameters * @ param name * @ param age * @ throws Exception */public void save (String name) throws Exception {SharedPreferences preferences = context. getSharedPreferences ("pathSet", Context. MODE_WORLD_READABLE); Editor editor = preferences. edit (); editor. putString ("name", name); editor. commit ();}/*** extract application parameters * @ return * @ throws Exception */public Map
GetPreferences () throws Exception {SharedPreferences preferences = context. getSharedPreferences ("pathSet", Context. MODE_WORLD_READABLE); Map
Result = new HashMap
(); Result. put ("name", preferences. getString ("name", ""); return result ;}}
Below is the activity
Public class PathSet extends Activity {private EditText pathNameText; private PreferencesService service; public PathSet (final Context context) {super (context); LayoutInflater inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); inflater. inflate (R. layout. pathset, this); service = new PreferencesService (context); pathNameText = (EditText) this. findViewById (R. id. pathName); Map
Params = null; try {params = service. getPreferences ();} catch (Exception e1) {// TODO Auto-generated catch blocke1.printStackTrace ();} pathNameText. setText (params. get ("name"); Button button = (Button) findViewById (R. id. btn_savePath); button. setOnClickListener (new View. onClickListener () {public void onClick (View v) {String name = pathNameText. getText (). toString (). trim (); if ("". equals (name) | name = null) {Toast. makeText (context, "path cannot be blank", 1 ). show ();} else {try {service. save (name); Toast. makeText (context, R. string. succss, 1 ). show ();} catch (Exception e) {Toast. makeText (context, R. string. fail, 1 ). show (); e. printStackTrace ();}}}});}}
The following figure shows the effect: