Android uses SharedPreferences to store and read data

Source: Internet
Author: User

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:



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.