In the Android program development we often encounter four kinds of data storage methods, each of which have different storage methods; Here are the features of different storage modes in Android development
One, Preferences
Preferences is a lightweight method of storing data, using the following methods:
Save values in a:
Sharedpreferences.editor sharedata = getsharedpreferences ("Data", 0). Edit ();
Sharedata.putstring ("name", "Shenrenkui");
Take a value in B:
Sharedpreferences sharedata = getsharedpreferences ("Data", 0);
String data = sharedata.getstring ("name", null);
Note that the parameters of the context.getsharedpreferences (String name,int type) are more the same as the data permission properties of the data when we create it, the process of storing and taking values is a bit like hashmap but more human than HashMap. , GetXXX (Object key,object defualtreturnvalue), and the second argument is when the key you want corresponds to a value that is not returned at any time. This eliminates a lot of logical judgment ....
Second, Files
The file that is not on Android is the purebred file in the J2SE, the visible function is powerful, here even is a cursory passing through seriously.
Create file
= new files (file_path, file_name);
File.createnewfile ();
Open OutputStream out
= new FileOutputStream (file);
String infotowrite = "on paper on the end feel shallow, know this matter to preach";
Converts a string into a byte array to write to a file
Out.write (Infotowrite.getbytes ());
Closes the file OutputStream
out.close ();
Open InputStream in
= new FileInputStream (file)
Reads all the contents of the file into the byte array
int length = (int) file.length ();
byte[] temp = new Byte[length];
In.read (temp, 0, length);
The byte array is encoded with UTF-8 and stored in the display string
display = encodingutils.getstring (temp,text_encoding);
Closes InputStream
in.close () of file files (
IOException e) {
//Prints error messages to Logcat
log.e (TAG, E.tostring ());
This.finish ();
}
Read
InputStream is=getresources (). Getrawresource (r.raw. FileName) from the resource
Three, Databases
Android has embedded a relational database that functions more powerful than other mobile operating systems, and the SQL statements we learned at college are basically available, and the data we create ourselves can be used in the ADB shell. Sqlite3 The specific path is/data/data/package_name/databases. For example, here is a demonstration of the operation below the Com.android.providers.media package.
1, adb Shell 2, cd/data/data/com.android.providers.media/databases 3, LS (view the database below Com.android.providers.media) 4,
Sqlite3 internal.db 5,. Help---See how to Operate 6,. table lists table 7 in internal data, select * from albums;
Databasehelper Mopenhelper;
private static final String database_name = "DBFORTEST.DB";
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) {super, Database_
NAME, NULL, database_version); @Override public void OnCreate (Sqlitedatabase db) {String sql = "CREATE TABLE" + table_name + "(" + TITLE + "Text No
T null, "+ body +" text NOT NULL "+"); ";
LOG.I ("haiyang:createdb=", SQL);
Db.execsql (SQL); @Override public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {}}/** * Re-establish the data table */private void C Reatetable () {Sqlitedatabase db= Mopenhelper.getwritabledatabase ();
String sql = "CREATE TABLE" + table_name + "(" + TITLE + "text NOT NULL," + body + "text NOT NULL" + ");";
LOG.I ("haiyang:createdb=", SQL); try {db.execsql ("DROP table IF EXISTS Diary"); Db.execsql (SQL); Settitle ("Successful rebuild of Datasheet");} catch (SQLException e) {settitle (
"Data table rebuild error");
}/** * Delete datasheet */private void droptable () {Sqlitedatabase db = Mopenhelper.getwritabledatabase ();
String sql = "DROP table" + table_name;
try {db.execsql (SQL); Settitle (datasheet deleted successfully: + SQL);} catch (SQLException e) {settitle ("datasheet delete error");}
/** * Insert two data/private void InsertItem () {Sqlitedatabase db = Mopenhelper.getwritabledatabase ();
String SQL1 = "INSERT INTO" + table_name + "(" + TITLE + "," + Body + ") VALUES (' Haiyang ', ' Android's development is really fast ');";
String sql2 = "INSERT INTO" + table_name + "(" + TITLE + "," + Body + ") VALUES (' Icesky ', ' Android's development is really fast ');";
try {log.i ("haiyang:sql1=", SQL1);
LOG.I ("haiyang:sql2=", SQL2);
Db.execsql (SQL1);
Db.execsql (SQL2); SettitLe ("Insert two data successfully");
catch (SQLException e) {settitle ("Insert two data Failed");} /** * Delete one of the data * * private void DeleteItem () {try {sqlitedatabase db = Mopenhelper.getwritabledatabase (); Db.delete (tabl
E_name, "title = ' Haiyang '", null);
Settitle ("Delete a record of title is Haiyang");
SQLException e) {}}/** * Displays the number of bars of data in the current datasheet in the title area of the screen.
* * private void Showitems () {Sqlitedatabase db = Mopenhelper.getreadabledatabase ();
String col[] = {TITLE, body};
Cursor cur = db.query (table_name, col, NULL, NULL, NULL, NULL, NULL);
Integer num = Cur.getcount ();
Settitle (integer.tostring (num) + "record"); }
Four, network
This is the use of the Internet to store the data we want, this is the CS structure storage mode, but also a point of name.
How to use the Content Provider
Below are several typical content provider applications that users often encounter:
* 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 settin GS and Preferences
Call the standard URI structure for the content provider resource:
<standard_prefix>://<authority>/<data_path>/<id>
For example:
1 obtain browser All "bookmark" Information: Content://browser/bookmarks
2 Obtain the information in the system Address Book: Content://contacts/people (if you get a specific communication record, specify an ID number at the end of the path URI: CONTENT://CONTACTS/PEOPLE/5
Simple instance Fragment:
Uri allcalls = Uri.parse ("Content://call_log/calls");
Cursor C = managedquery (allcalls, NULL, NULL, NULL, NULL);
The above content is small series to introduce the Android four kinds of storage way, hope everybody likes, more information please log on cloud Habitat community website to know more.