Android-SharedPreferences, sharedpreferences

Source: Internet
Author: User

Android-SharedPreferences, sharedpreferences

[Return directory]

SharedPreferences provides data storage in the form of "Key = value ".

The information saved by android. content. SharedPreferences can only be of some basic data types, such as string, integer, and Boolean.

SharedPreferences stores configuration files. The default file suffix is *. xml, which is the same as the Properties class in Java (only basic data types can be saved)

Chinese characters cannot be saved, and Chinese characters must be transcoded.

By default, all configuration files are saved in the system folder, under/data/package name/shared prefs

Window-> show View-> Others-> File Explorer allows you to View system folders.


For example:

<Span style = "font-family: SimHei; font-size: 18px;"> package com. example. testsharedpreferences; import android. app. activity; import android. content. sharedPreferences; import android. OS. bundle; public class MainActivity extends Activity {private final String FILENAME = "potato"; // save the file name and generate potato. xml file @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); SharedPreferences sharedPreferences = super. getSharedPreferences (FILENAME, Activity. MODE_PRIVATE); SharedPreferences. editor edit = sharedPreferences. edit (); edit. putString ("name", "potato"); edit. putInt ("age", 22); edit. putBoolean ("isStudent", true); edit. commit () ;}</span>

Add a potato. xml file under/data/com. example. testsharedpreferences/shared prefs in the system folder.

The SharedPreferences interface is used to store data in SharedPreferences. Editor.


The following describes how to read SharedPreferences:

<Span style = "font-family: SimHei; font-size: 18px;"> package com. example. testsharedpreferences; import android. app. activity; import android. content. sharedPreferences; import android. OS. bundle; import android. util. log; public class MainActivity extends Activity {private final String FILENAME = "potato"; // save the file name and generate potato. xml file @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); SharedPreferences sharedPreferences = super. getSharedPreferences (FILENAME, Activity. MODE_PRIVATE); Log. e ("Potato", "Name:" + sharedPreferences. getString ("name", "I am the default value"); Log. e ("Potato", "Age:" + sharedPreferences. getInt ("age", 1); Log. e ("Potato", "isStudent:" + sharedPreferences. getBoolean ("isStudent", false) ;}</span>





Sharedpreferences of Android

Deleting an xml file cannot completely delete SharedPreferences. For details, refer to the android source code. This item is static. As long as it is not recycled, the original data can still be read in the cache.
You can delete the file by using the clear editor and using the commit command. It cannot be so violent ............

Encapsulation of android SharedPreferences

Here I have encapsulated it for you. Blog.csdn.net/..542944 only encapsulates the string and int types. You can take a look .... Make sure you have a satisfactory answer. I wrote it for a while.
 

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.