Sharedpreferences for Small Data Storage

Source: Internet
Author: User

Sharedpreferences are often used to store small data, such as logging the user name and password locally. Of course, its data is stored locally. If the application data is cleared, the stored data here is gone.

I have done this before to determine whether the user is the first to enter the software after installation. This is used for data storage. This is another requirement in the projects I have made during this period, A tool class for sharedpreferences storage is provided below.

public class Editer {    Context ct;    SharedPreferences preferences;    public Editer(Context ct)    {        this.ct=ct;    }        public Boolean saveinfo(String name, String key,String value)    {        preferences =  ct.getSharedPreferences(name,1);        Editor editor = preferences.edit();            editor.putString(key, value);        Boolean bres= editor.commit();             return bres;        }            public String getinfo(String name,String key)    {        String res="";        preferences =  ct.getSharedPreferences(name,1);        res=preferences.getString(key, "");        return res;    }}

Call in activity:

Editer ed = new Editer(MainActivity.this);

Then, call the corresponding storage and read the method.
Storage Method

Ed. saveinfo ("iport", "ip", ip); iport indicates the tag value, IP indicates the tag value, and IP indicates the value you want to store.

Obtain Method

ed.getinfo("iport", "ip")

Here, the stored IP value is taken out.

Delete Method

 localOrder.prefs.edit().clear().commit();

 

 

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.