Android --- 34 --- SharedPreferences, sharedpreferences
SharedPreferences stores data similar to the configuration information format. Therefore, SharedPreferences stores data in simple k-v pairs.
The SharedPreferences interface is mainly used to read the Preferences data of an application. It provides the following common methods:
The SharedPreferences interface does not provide the ability to write data. Instead, it calls the edit () method to obtain the corresponding Editor object through the internal interface of SharedPreferences. The following methods are provided to write data.
Commit: After the Editor is edited, call this method to submit the modification.
From the usage perspective, the combination of SharedPreferences and Editor is very similar to Map, where SharedPreferences is used to read data, while the Editor is used to write data.
Create a SharedPreferences:
It is an interface and cannot directly create instances. It can only obtain SharedPreferences instances through getSharedPreferences (String name, int mode) provided by Contex. The second parameter supports the following values:
Contex. MODE_PRIVATE: specifies that the SharedPreferences data can only be read and written by this application.
Contex. MODE_WORLD_READABLE: specify that the SharedPreferences data can be read by other applications, but cannot be written.
Contex. MODE_WORLD_WRITEABLE: specify that the SharedPreferences data can be read and written by other applications.
Example: writing and reading random numbers
There are only two buttons in the program, one for writing data and the other for reading data. Therefore, no layout code is provided.
Import java. text. simpleDateFormat; import java. util. date; import android. app. activity; import android. content. sharedPreferences; import android. OS. bundle; import android. preference. preference; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast;/***** @ author Caesar ***/public groovy Ss MainActivity extends Activity {SharedPreferences preferences; SharedPreferences. editor editor; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); preferences = getSharedPreferences ("crazyit", MODE_WORLD_READABLE); editor = preferences. edit (); Button read, write; write = (Button) findViewById (R. id. write); read = (Butto N) findViewById (R. id. read); read. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stub // read String data String time = preferences. getString ("time", null); // read int-type data int randomNum = preferences. getInt ("random", 0); String result = time = null? "You have not yet written data": "write time:" + time + "\ n:" + randomNum; Toast. makeText (MainActivity. this, result, 0 ). show () ;}}); write. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubSimpleDateFormat sdf = new SimpleDateFormat ("MM dd, yyyy" + "hh: mm: ss"); // save it to the editor at the current time. putString ("time", sdf. format (new Date (); // store a random number editor. putInt ("random", (int) (Math. random () * 100); // submit all stored data editor. commit ();}});}}
The number of times the program is used:
Import android. app. activity; import android. content. sharedPreferences; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. widget. toast;/*** record Application Usage: ** @ author Caesar **/public class MainActivity extends Activity {SharedPreferences preferences; SharedPreferences. editor editor; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); preferences = getSharedPreferences ("count", MODE_WORLD_READABLE); editor = preferences. edit (); int count = preferences. getInt ("count", 0); Toast. makeText (MainActivity. this, "the program has been used:" + count + "Times", 0 ). show (); editor. putInt ("count", ++ count); editor. commit ();}}
Read and Write SharedPreferences of other applications:
Prerequisite: Specify Permissions
Steps:
1. Create the Contex corresponding to other programs
2. Call the getSharedPreferences (String name, int mode) of COntex of other applications to obtain the SharedPreferences object in the response.
3. To write data to SharedPreferences of other applications, call the edit Method of SharedPreferences to obtain the response Editor.
Import android. app. activity; import android. content. context; import android. content. sharedPreferences; import android. content. pm. packageManager. nameNotFoundException; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. widget. textView;/***** @ author Caesar **/public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Context useCount = null; try {// get the ContexuseCount = createPackageContext ("com. example. sharedpreferencesdemo2 ", Context. CONTEXT_IGNORE_SECURITY);} catch (NameNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} // use the Contex of another program to obtain the corresponding SharedPreferencesSharedPreferences preferences = useCount. getSharedPreferences ("count", Context. MODE_WORLD_READABLE); int count = preferences. getInt ("count", 0); TextView show = (TextView) findViewById (R. id. show); show. setText ("SharedPreferencesDemo2 has been used" + count + "times ");}}