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.