android--using Sharedpreference to store data

Source: Internet
Author: User

public interface Sharedpreferences

Import:

Android.content.SharedPreferences;

Overiview:

Mainly used to store some of the APK's property data, such as: Window size, volume and other preference data.

Main methods:

  

Use:

(1) Obtaining an instance of Sharedpreferences via Getsharedpreferences (string name,int mode)

(2) If you need to deposit data, use Sharedpreferences.editor to get an editor using editor.putstring (string key,string value)

Editor.commit () Deposit data

(3) Read the data using the obtained instance. GetString (String key,string "");//second parameter default value

code example:

A) Data deposit

 //instantiating a Sharedpreferences object (first step)Sharedpreferences mysharedpreferences= getsharedpreferences ("test", activity.mode_private); //instantiating a Sharedpreferences.editor object (step two)Sharedpreferences.editor Editor =Mysharedpreferences.edit ();//Save the data in a putstring wayEditor.putstring ("name", "Karl"); Editor.putstring ("Habit", "sleep"); //Submit Current Dataeditor.commit ();//To write data successfully using the TOAST information hint boxToast.maketext ( This, "data successfully written to sharedpreferences! ", Toast.length_long). Show (); 

b) read out the data

sharedpreferencessharedpreferences= getsharedpreferences (  "test"//  String name =sharedpreferences.getstring ("name", """=sharedpreferences.getstring (" Habit ","""/ /toast.maketext (This, "read the data as follows:" + "\ n" + "name:" + name + "\ n" + "habit:" +

android--using Sharedpreference to store data

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.