Most of the time we develop software needs to provide users with software parameter setting functions, such as our common QQ, users can set whether to allow strangers to add themselves as friends. For the preservation of software configuration parameters, if the window software is usually used to save the INI file, if it is a J2SE application, we will use the Properties attribute file or XML to save. If it's an Android app, what's the best way to save software configuration parameters? The Android platform provides us with a sharedpreferences class, which is a lightweight storage class that is especially suitable for saving software configuration parameters. Use Sharedpreferences to save the data, behind it is the XML file to hold the data, the file is stored in the/data/data/<package name>/shared_prefs directory.
The following is the default path setting, which illustrates its usage, first, the layout file:
<?xml Version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" > < TextView android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "@string/hello" /> <edittext android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:id= "@+ Id/pathname "/><buttonandroid:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:tex t= "@string/button" android:id= "@+id/btn_savepath"/><textview android:id= "@+id/explaition" android:layout _width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/pathset_explation"/></ Linearlayout>
Here 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;} /** * Implement application Parameters Save * @param name * @param age * @throws Exception */public void Save (String name) throws Exception{sharedprefe Rences preferences=context.getsharedpreferences ("Pathset", context.mode_world_readable); Editor Editor=preferences.edit (); editor.putstring ("name", name); Editor.commit ();} /** * Implementation of application Parameters extraction * @return * @throws Exception */public map<string, string> getpreferences () throws EXCEPTION{SHAREDPR Eferences preferences=context.getsharedpreferences ("Pathset", context.mode_world_readable); Map<string, string> result=new hashmap<string, string> (); Result.put ("Name", Preferences.getstring ("name "," ")); return result;}}
here 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);p athnametext= (EditText) This.findviewbyid (R.id.pathname); map<string, string> params = null;try {params = Service.getpreferences ();} catch (Exception E1) {//TODO Auto-genera Ted 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 empty", 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 ();}}}); }}
Here are the effects:
Android uses sharedpreferences for data storage and data reading