Usage Analysis of Android SharedPreferences

Source: Internet
Author: User

SharedPreferences
It is used to provide software parameter settings when developing software. The xml file is used to store data, and the file is stored in the/data/<package name>/shared_prefs directory;

Copy codeThe Code is as follows:
Public void savePreferences (String name, Integer age ){
--> Get SharedPreferences
SharedPreferences preferences = context. getSharedPreferences ("itcase", Context. MODE_PRIVATE );
// You do not need to specify the xml Suffix of the file name. Because the system knows that it is an xml file, android will automatically add it;
Editor editor = preferences. edit (); // the data is stored in the memory;
Editor. putString ("name", name );
Editor. putInt ("age", age );
Editor. commit (); // This method must be used to submit internal data back to the file
}

Public Map <String, String> getPreferences (){
Map <String, String> map = new HashMap <String, String> ();
SharedPreferences preferences = context. getSharedPreferences ("itcase ",
Context. MODE_PRIVATE); // you do not need to specify the xml extension name because the system knows it is an xml file;
Map. put ("name", preferences. getString ("name", "default"); // if no parameter exists, the default value is returned.
Map. put ("age", String. valueOf (preferences. getInt ("age", 0 )));
Return map;
}

--> Get SharedPreferences

// This. getPreferences (mode); // by default, the activity name is used as the xml name;

// PreferenceManager. getdefasharsharedpreferences (Context );

**************************************** **************

Android: onClick = "save" <! -- This parameter is used to specify a method name. It must be defined in acvitity on the displayed interface. The save method must be the same as the Onclick signature, as shown below! -->

View. OnClickListener (){

Public void onClick (View v ){}

}

Public void save (View v) {}// definition in Activity;

Android: numeric = "integer" <! -- Set the value type -->

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.