All applications must involve data input and output, and Android applications are no exception. The parameter settings and program running status data of applications must be stored in the external storage, in this way, data will not be lost after the system is shut down. Of course, different storage methods should be selected for the amount of data to be operated. Therefore, the Android system provides four Data Storage Methods: SharedPreferences, File, SQLite, and ContentProvider.
SharedPreferences: In addition to the SQLite database, SharedPreferences is also a lightweight data storage method. Its essence is to store key-value pairs based on XML files, it is usually used to store some simple configuration information. The SharedPreferences interface does not provide the ability to write data. Instead, it uses the internal interface of SharedPreferencesde to obtain the corresponding Editor object by calling the edit () method. To store SharedPreferences, follow these steps:
1. Get the SharedPreferences object
2. Use the edit () method to obtain the Editor object
3. Store key-value pairs of data through the Editor object
4. submit data using the commit () method
Example 1: The following code stores three data types: String, Int, and Boolean, as shown below:
package com.ye_yun_lin.sharedpreferencestest;import android.os.Bundle;import android.preference.Preference;import android.app.Activity;import android.content.SharedPreferences;import android.view.Menu;public class MainActivity extends Activity {private SharedPreferences sharedPreferences;private SharedPreferences.Editor editor;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);sharedPreferences=getSharedPreferences("My_XML", MODE_WORLD_READABLE);editor=sharedPreferences.edit();editor.putString("String_key", "string");editor.putInt("Int_key", 3);editor.putBoolean("Boolean_key", true);editor.commit();}}
After the code is executed, an SP. My_XML file is generated under the/data/com. ye_yun_lin.SharedPrefer/shared_prefs directory. An application can create multiple such files. Use the export button on the DDMS interface to export the file and open it as follows:
Example 2: record the Application Usage:
This instance can remember the number of times the application is used. When the user starts the program for the first time, the system creates a SharedPreferences to record the usage. When you start the application later, the system first reads the number of times used in SharedPreferences, and then adds the number of times to 1. The Code is as follows:
Package com. ye_yun_lin.usecount; import android. r. integer; import android. OS. bundle; import android. preference. preference; import android. app. activity; import android. content. sharedPreferences; import android. view. menu; import android. widget. toast; public class MainActivity extends Activity {private SharedPreferences sharedPreferences; private SharedPreferences. editor editor; private int count; @ Overrideprote Cted void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); sharedPreferences = getSharedPreferences ("count", MODE_WORLD_WRITEABLE); editor = sharedPreferences. edit (); count = sharedPreferences. getInt ("count", 0); Toast. makeText (this, "this application is used" + count + "times! ", Toast. LENGTH_LONG). show (); editor. putInt (" count ", ++ count); editor. commit ();}}
Example 3: Read and Write SharedPreferences from other applications.
To read or write the SharedPreferences of other programs, you must create the application and specify the corresponding access permissions. For example, MODE_WORLD_READABLE (which can only be read by other programs) and MODE_WORLD_WRITABLE (which can be read or written by other programs). The steps are as follows:
1. Create the Context corresponding to other programs, for example, the following code:
useContext=createPackageContext("com.ye_yun_lin.usecount", Context.CONTEXT_IGNORE_SECURITY);
The above com. ye_yun_lin is the package name of other programs
2. Use getSharedPreferences () of the Context of another program to obtain the corresponding SharedPreferences object.
3. To write data to SharedPreferences of other applications, call the edit () method of SharedPreferences to obtain the corresponding editor.
The Code is as follows:
package comye_yun_line.useotherpreferences;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.content.pm.PackageManager.NameNotFoundException;import android.view.Menu;import android.widget.TextView;public class MainActivity extends Activity {private Context useContext=null;private SharedPreferences preferences;private int count;private TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);try {useContext=createPackageContext("com.ye_yun_lin.usecount", Context.CONTEXT_IGNORE_SECURITY);} catch (NameNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}preferences=useContext.getSharedPreferences("count", Context.MODE_WORLD_READABLE);count=preferences.getInt("count", 0);textView=(TextView)findViewById(R.id.textview);textView.setText(count+"");}}
Compared with SQLite databases, SharedPreferences removes the need to create databases, create tables, and write SQL statements, making SharedPreferences more convenient and concise. However, SharedPreferences also has its own defects. For example, SharedPreferences can only store five simple data types: boolean, int, string, float, and long. For example, SharedPreferences cannot be used for conditional queries. Therefore, no matter how simple the SharedPreferences data is to be stored, it can only be a supplement to the storage method, but it cannot completely replace the storage method like SQLite database.