In Android program development, we often encounter four data storage methods, each storage method is different; below I list the characteristics of different storage methods in Android development respectively
1. Preferences
Preferences is a lighter method to store data. The specific usage method is:
Save the value in A:
sharedata = getSharedPreferences("data", 0).edit(); ("name","shenrenkui"); ();
Take the value in B:
SharedPreferences sharedata = getSharedPreferences("data", 0); String data = ("name", null); (TAG,"data="+data);
Note that the (String name, int type) parameter is the same when we create data. The process of storing and getting values is a bit like HashMap, but it is more humane than HashMap. getXXX(Object key,Object defaultReturnValue), the second parameter is the value returned when the key you want does not correspond to. This saves a lot of logical judgments. . . .
2. Files
The File not available on Android is the purebred File in J2se. It can be seen that the function is so powerful that it is a very rare passerby here.
//Create a filefile = new File(FILE_PATH , FILE_NAME); (); //Open the OutputStream of the file fileout = new FileOutputStream(file); String infoToWrite = "It is always shallow to know what you know on paper, and you must practice it yourself"; //Convert string to byte array to write to file(()); //Close the OutputStream of the file file(); //Open the InputStream of the file filein = new FileInputStream(file); //Read all the file contents into the byte arrayint length = (int)(); byte[] temp = new byte[length]; (temp, 0, length); //Encode the byte array in UTF-8 and store it in the display stringdisplay = (temp,TEXT_ENCODING); //Close the InputStream of the file file(); } catch (IOException e) { //Print the error message to Logcat(TAG, ()); (); } //Read from the resourceInputStream is=getResources().getRawResource(.file name)
3. Databases
Android has a relational database SQLite3 that is built-in with a powerful function than other mobile phone operating systems. The SQL statements we learned in college can basically be used, and the data we created ourselves can be operated with an adb shell. The specific path is /data/data/package_name/databases. For example, here we demonstrate the operation below the package.
1, adb shell 2, cd /data/data//databases 3, ls(View the database below) 4, sqlite3 5, .help---See how to operate 6, .tableListinternalTables in the data 7, select * from albums; DatabaseHelper mOpenHelper; private static final String DATABASE_NAME = ""; private static final int DATABASE_VERSION = 1; private static final String TABLE_NAME = "diary"; private static final String TITLE = "title"; private static final String BODY = "body"; private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String sql = "CREATE TABLE " + TABLE_NAME + " (" + TITLE + " text not null, " + BODY + " text not null " + ");"; ("haiyang:createDB=", sql); (sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } /** * Recreate the data table */ private void CreateTable() { SQLiteDatabase db = (); String sql = "CREATE TABLE " + TABLE_NAME + " (" + TITLE + " text not null, " + BODY + " text not null " + ");"; ("haiyang:createDB=", sql); try { ("DROP TABLE IF EXISTS diary"); (sql); setTitle("Data table rebuilt successfully"); } catch (SQLException e) { setTitle("Data table reconstruction error"); } } /** * Delete the data table */ private void dropTable() { SQLiteDatabase db = (); String sql = "drop table " + TABLE_NAME; try { (sql); setTitle("Data table was deleted successfully:" + sql); } catch (SQLException e) { setTitle("Data table deletion error"); } } /** * Insert two pieces of data */ private void insertItem() { SQLiteDatabase db = (); String sql1 = "insert into " + TABLE_NAME + " (" + TITLE + ", " + BODY + ") values('haiyang', 'Android is developing so quickly');"; String sql2 = "insert into " + TABLE_NAME + " (" + TITLE + ", " + BODY + ") values('icesky', 'Android is developing so quickly');"; try { ("haiyang:sql1=", sql1); ("haiyang:sql2=", sql2); (sql1); (sql2); setTitle("Insert two pieces of data successfully"); } catch (SQLException e) { setTitle("Inserting two pieces of data failed"); } } /** * Delete one of the data */ private void deleteItem() { try { SQLiteDatabase db = (); (TABLE_NAME, " title = 'haiyang'", null); setTitle("Delete a record whose title is haiyang"); } catch (SQLException e) { } } /** * Displays the number of data in the current data table in the title area of the screen. */ private void showItems() { SQLiteDatabase db = (); String col[] = { TITLE, BODY }; Cursor cur = (TABLE_NAME, col, null, null, null, null, null); Integer num = (); setTitle((num) + "Record"); }
4. Network
This is to use the Internet to store the data we want. This is the storage method of the CS structure, and it is also a click on the name.
How to use Content Provider
Below are several typical Content Provider applications that users often come into contact with:
* Content Provider Name : Intended Data * Browser : Browser bookmarks, Browser history, etc. * CallLog : Missed calls, Call datails, etc. * Contacts : Contact details * MediaStore : Media files such as audio, Video and Images * Settings : Device Settings and Preferences
The standard URI structure for calling Content Provider resources:
<standard_prefix>://<authority>/<data_path>/<id>
For example:
1) Obtain all "bookmarks" information of the browser: content://browser/bookmarks
2) Get the information in the system address book: content://contacts/people (If you obtain a specific communication record, specify an ID number at the end of the path URI: content://contacts/people/5
Simple example snippet:
Uri allCalls = ("content://call_log/calls"); Cursor c = managedQuery(allCalls, null, null, null, null);
The above content is the four Android storage methods introduced to you by the editor. I hope you like it. For more information, please log in to my website to learn more.