Android-storage of configuration files SharedPreferences for data storage

Source: Internet
Author: User

Android-storage of configuration files SharedPreferences for data storage

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: SharedPreferences sharedPreferences = getSharedPreferences ("itcast", Context. MODE_PRIVATE); Editor editor = sharedPreferences. edit (); // get the editor. putString ("name", "Yang Chao"); editor. putInt ("age", 4); editor. commit (); // submit the modified generated itcast. the content of the xml file is as follows: Yang Chao Because SharedPreferences uses an xml file to save data, the first parameter of the getSharedPreferences (name, mode) method is used to specify the name of the file. The name does not need a suffix, And the suffix will be automatically added by Android. The second parameter of the method specifies the file operation mode. There are four operation modes. the preceding four modes are described when you use the File mode to save data. If you want the xml file used by SharedPreferences to be read and written by other applications, you can specify the Context. MODE_WORLD_READABLE and Context. MODE_WORLD_WRITEABLE permissions. In addition, Activity provides another getPreferences (mode) method to operate SharedPreferences. By default, This method uses the Class Name of the current class without the package name as the file name. L
The data code used to access SharedPreferences is as follows: SharedPreferences sharedPreferences = getSharedPreferences ("itcast", Context. MODE_PRIVATE); // getString () The second parameter is the default value. If the key does not exist in the preference, the default value String name = sharedPreferences is returned. getString ("name", ""); int age = sharedPreferences. getInt ("age", 1); If you access Preference from other applications, the precondition is that Context is specified when the preference is created. MODE_WORLD_READABLE or Context. MODE_WORLD_WRITEABLE permission. For example The preference is created for the cn. itcast. action application using the following statement. GetSharedPreferences ("itcast", Context. MODE_WORLD_READABLE );To access the preference of the above application, other applications must first create the Context of the above application and then access the preference through the Context. when accessing the preference, the preference will be found in the shared_prefs directory under the package where the application is located: context othersponcontext = createPackageContext ("cn. itcast. action ", Context. CONTEXT_IGNORE_SECURITY); SharedPreferences sharedPreferences = other1_context. getSharedPreferences ("itcast", Context. MODE_WORLD_READABLE); String name = sharedPreferences. getString ("name", ""); int age = sharedPreferences. getInt ("age", 0); if you do not create a Context to access preference of other applications, you can also directly access the xml file corresponding to preference of other applications by reading the xml file, such: file xmlFile = new File ("/data/ /Shared_prefs/itcast. xml ");// Replace with application package name l
------------------------------------------------------------

/**

* Save the parameters of the settings file.

*@ AuthorAdministratorYangchao

*/

Public ClassMainActivityExtendsActivity {

EditText etName, etAge, etContent;

// Define a SharedPreferences used to save data.XmlFile storage data,

SharedPreferences preferences;

@ Override

Protected VoidOnCreate (Bundle savedInstanceState ){

Super. OnCreate (savedInstanceState );

SetContentView (R. layout.Activity_main);

EtName = (EditText) findViewById (R. id.EtName);

EtAge = (EditText) findViewById (R. id.EtAge);

EtContent = (EditText) findViewById (R. id.EtContent);

/**

* Get SharedPreferences

* Use SharedPreferences to save data.XmlFile storage data,

* The file is stored in/data/ /Shared_prefs directory:

*/

Preferences =This. GetSharedPreferences ("prefer ",MODE_PRIVATE);

}

/**

* Save data

*@ ParamView

*/

Public VoidClick (View view)

{

String name = etName. getText (). toString (). trim ();

String age = etAge. getText (). toString (). trim ();

Editor editor = preferences. edit (); // get the Editor

Editor. putString ("name", name );

Editor. putInt ("age ",NewInteger (age ));

Editor. commit (); // submit dataPermanently save data

Toast.MakeText(This, "Saved successfully", 1). show ();

}

/**

* Reading data

*@ Param View

*/

Public VoidRead (View view)

{

String name = preferences. getString ("name", "default name ");

IntAge = preferences. getInt ("age", 000 );

EtContent. setText ("name:" + name + ", age:" + age );

}

}





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.