Sharedpreferences Storage of Android data storage

Source: Internet
Author: User

The Android system provides a system-level Configuration Storage scheme for applications, which is implemented by the Sharedpreferences interface, where all the information stored in the interface is saved in the form of a name-value pair, but its data types are limited to basic data types such as strings, shapes, booleans, and so on. These configurations are eventually saved in an XML file, and each time the app is opened, the saved information is loaded, and we can also purge the cached data in the management application.

The common methods for sharedpreferences interfaces are as follows:

Sharedpreferences Interface class method
No Method Type Describe
1 Public abstract Sharedpreferences.editor Edit () Ordinary Make it editable, and get the editor object
2 Public abstract Boolean contains (String key) Ordinary Determine if key exists
3 public abstract Map<string,?>getall () Ordinary Get all the data
4 Public abstract Boolean Getboolean (String key, Boolean defvalue) Ordinary Gets the Boolean value, if none, is set to Defvalue
5 public abstract float GetFloat (String key, float defvalue) Ordinary Get float value, if none, set to Defvalue
6 Puclic abstract int getInt (String key, int defvalue) Ordinary get int value, if none, set to Defvalue
7 Public abstract long Getlong (String key, long Defvalue) Ordinary Get a Long value, if none, set to Defvalue
8 Public abstract string GetString (string key, String defvalue) Ordinary Get sting value, if none, then set to Defvalue

The method of the Sharedpreferences interface GetXXX () is used to obtain the value that has already been stored. To write a new name value pair, you need to get its editor interface Sharedpreferes.editor interface, which is called the edit () method of the Sharedpreferences interface.

The common methods for Sharedpreferences.editor interfaces are as follows:

No Method Type Describe
1 Public abstract Sharedpreferences.editor Clear () Ordinary Clear All data
2 Public abstract Boolean commit () Ordinary Submit the updated data
3 Public abstract Sharedpreferences.editor Putboolean (String key, Boolean value) Ordinary To save a Boolean type of data
4 Public abstract Sharedpreferences.editor putfloat (String key, float value) Ordinary Save float type data
5 Public abstract Sharedpreferences.editor Putint (String key, int value) Ordinary Save int type data
6 Public abstract Sharedpreferences.editor Putlong (String key, Long value) Ordinary Save long type data
7 Public abstract Sharedpreferences.editor putstring (string key, String value) Ordinary Save string Type data
8 Public abstract Sharedpreferences.editor Remove (String key) Ordinary Delete data from the specified key

Since both Sharedpreferences and sharedpreferences.editor are interfaces, it is also necessary to support the methods and constants of the activity class to obtain their instantiated objects.

public static final int mode_private, public static final int Mode_world_readbale, public static final int Mode_world_writ The Ebale constant and public sharedpreferences getsharedpreferences (String name, int mode) (where name refers to the saved file name, mode refers to the pattern of the operation).

This is illustrated by an example:

1  Packagecom.wl.testsharedpreferences;2 3 ImportAndroid.os.Bundle;4 Importandroid.app.Activity;5 Importandroid.content.SharedPreferences;6 ImportAndroid.view.Menu;7 ImportAndroid.view.TextureView;8 ImportAndroid.view.View;9 ImportAndroid.view.View.OnClickListener;Ten ImportAndroid.widget.Button; One ImportAndroid.widget.TextView; A  -  Public classMainactivityextendsActivityImplementsOnclicklistener { -  the Button btn1,btn2; -Sharedpreferences SP =NULL; -      - @Override +     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); ABTN1 =(Button) Findviewbyid (R.ID.BTN1); atBTN2 =(Button) Findviewbyid (R.ID.BTN2); -          -Btn1.setonclicklistener ( This); -Btn2.setonclicklistener ( This); -          -SP = getsharedpreferences ("MYSP", activity.mode_private); Get Sharedpreferences Instances in     } -  to  + @Override -      Public voidOnClick (view view) { the         //TODO auto-generated Method Stub *TextView TV =(TextView) Findviewbyid (r.id.tv); $         if(View = =btn1) {Panax NotoginsengSharedpreferences.editor Editor =Sp.edit (); -Editor.putstring ("Author", "Xiaowang"); theEditor.putint ("Age", 25); + editor.commit (); ATv.settext ("Write successfully!"); the}Else if(View = =btn2) { +Tv.settext (sp.getstring ("Author", "Xiaozhang") + "," + sp.getint ("Age", 20)); -         } $     } $}
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Fill_parent"4 Android:layout_height= "Fill_parent"5 android:orientation= "vertical" >6 7     <TextView8         Android:id= "@+id/tv"9 Android:layout_width= "Fill_parent"Ten Android:layout_height= "Wrap_content"/> One      A     <Button -         Android:id= "@+id/btn1" - Android:layout_width= "Fill_parent" the Android:layout_height= "Wrap_content" - Android:text= "@string/btn1"/>//string/btn1 as "write name value pair" -      -     <Button +         Android:id= "@+id/btn2" - Android:layout_width= "Fill_parent" + Android:layout_height= "Wrap_content" A Android:text= "@string/btn2"/>//string/btn2 as "read Name value pair" at  - </LinearLayout>

From the above example can easily grasp the use of sharedpreferences.

Note: When we write a name value pair, we must remember to call the commit () function, otherwise the write is not committed, it will not be read out.

Sharedpreferences storage for Android data storage

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.