Directory:
I. Use SharedPreferences to store Application Data
II. Store application data in internal storage
III. Store application data in External Storage
IV. Data Storage through SQLite Database
- SQLiteDatabase class
- Cursor Interface
- SQLiteOpenHelper class
- Procedure
V. Content Provider Introduction
I. Use SharedPreferences to store application data:
Reference: Beginning Android 4 Application Development-Page 252
- Store the data to the SharedPreferences object. In this case, the SharedPreferences. Editor method is used.
- The preceding process stores data in an xml file.
In the DDMS view, find File e and select data/<PackageName>/shared_prefs/*. xml
As shown in:
- Use the getSharedPreferences () method to obtain the SharedPreferences object and retrieve the data.
Example:
Code function: You can store text content by clicking the "Modify text" button, and then click "show text" to display the modified text.
In addition, other activities can also be called directly. The call method is the same as the following ~
1 public class MainActivity extends Activity { 2 3 @Override 4 public void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 8 Button displayButton = (Button)findViewById(R.id.button1); 9 displayButton.setOnClickListener(new OnClickListener() {10 11 public void onClick(View v) {12 // TODO Auto-generated method stub13 SharedPreferences appPreferences = getSharedPreferences(14 "com.example.preferenceexer_preferences", MODE_PRIVATE);15 Toast.makeText(MainActivity.this, appPreferences.getString("editTextPref", ""), Toast.LENGTH_SHORT).show();16 }17 });18 19 Button modifyButton = (Button)findViewById(R.id.button2);20 modifyButton.setOnClickListener(new OnClickListener() {21 22 public void onClick(View v) {23 // TODO Auto-generated method stub24 SharedPreferences appPreferences = getSharedPreferences(25 "com.example.preferenceexer_preferences", MODE_PRIVATE);26 SharedPreferences.Editor prefsEditor = appPreferences.edit();27 prefsEditor.putString("editTextPref", ((EditText)findViewById(R.id.editText1)).getText().toString());28 prefsEditor.commit();29 }30 });31 }32 }
SharedPreferences interface:
1. Interface for accessing and modifying preference data returnedGetSharedPreferences(String, int ).
GetSharedPreferences(String, int): Return Value: SharedPreferences
The first parameter is the name of the storage file, which is generally in the format of <PackageName> _ preferences. Other parameters are also acceptable.
The second parameter: 0 indicates MODE_PRIVATE, which can only be used by this program. There are other constants.
2. SharedPreferences Methods:
- Edit(): Return Value: SharedPreferences. Editor. Enter the editing mode.
- Contains(String key): Check whether a specified key exists.
- GetAll(): Return Value: Map <String,?>. Retrieve all values from preferences.
- GetBoolean(String key, boolean defValue): Retrieves a boolean value. The second value is the default value.
- GetFloat(String key, float defValue ):
- GetInt(String key, int defValue ):
- GetLong(String key, long defValue ):
- GetString(String key, String defValue): Retrieves a String. The second parameter is the default value.
- GetStringSet(String key, Set <String> defValues ):
SharedPreferences. Editor interface:
1. Used to modify the value of the SharedPreferences object.
2. SharedPreferences. Editor Methods:
- Commit(): The content to be modified.
- Apply(): Same as above.
- Remove(String key): deletes a specified key.
- Clear(): Clear all.
- PutBoolean(String key, boolean value): Add a boolean value.
- PutFloat(String key, float value ):
- PutInt(String key, int value ):
- PutLong(String key, long value ):
- PutString(String key, String value): adds a String.
- PutStringSet(String key, Set <String> values ):
II. Store application data in internal memory:
Reference: Beginning Android 4 Application Development-Page 263
:
In the DDMS view, select data/<PackageName>/files/textfile.txt.
As shown in:
Public class FilesActivity extends Activity {EditText textBox; static final int READ_BLOCK_SIZE = 100; // character array @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); textBox = (EditText) findViewById(R.id.txt Text1);} public void onClickSave (View view) {String str = textBox. getText (). toString (); try {FileOutputStream fOut = o PenFileOutput ("textfile.txt", MODE_WORLD_READABLE); // output file stream OutputStreamWriter osw = new OutputStreamWriter (fOut); osw. write (str); osw. close (); Toast. makeText (getBaseContext (), "File saved successfully! ", Toast. LENGTH_SHORT ). show (); textBox. setText ("");} catch (IOException ioe) {ioe. printStackTrace () ;}} public void onClickLoad (View view) {try {FileInputStream fIn = openFileInput ("textfile.txt"); // file write stream InputStreamReader isr = new InputStreamReader (fIn ); char [] inputBuffer = new char [READ_BLOCK_SIZE]; String s = ""; int charRead; while (charRead = isr. read (inputBuffer)> 0) {String readStr Ing = String. copyValueOf (inputBuffer, 0, charRead); s + = readString; inputBuffer = new char [READ_BLOCK_SIZE];} textBox. setText (s); Toast. makeText (getBaseContext (), "File loaded successfully! ", Toast. LENGTH_SHORT). show () ;}catch (IOException ioe) {ioe. printStackTrace ();}}}
Download source file: Files.zip
III. Store application data in External Storage:
Reference: Beginning Android 4 Application Development-Page 268
: