1 Use SharedPreferences to store data.
2 file storage data
3 SQLite database stores data
4 Use ContentProvider to store data
5 network storage data
Different business logic or requirements are implemented in different ways. The following are the explanations and methods of these data storage methods:
First, use SharedPreferences to store data.
SharedPreferences is a lightweight storage class on the Android platform, which mainly stores some commonly used configurations, such as window state. Generally, the window state is overloaded in Activity and saved by SharedPreferences, which provides conventional long Integer, int and String saving on Android platform.
The following is sample code:
Public class MainActivity extends Activity {
@ Overlay
public void on create(Bundle saved instancestate){
super . oncreate(savedInstanceState);
setContentView(r . layout . main);
//Get the SharedPreferences object
Context ctx = MainActivity.this
shared preferences SP = CTX . getsharedpreferences(" SP ",MODE _ PRIVATE);
//store data
editor editor = sp . edit();
editor.putString("STRING_KEY "," STRING ");
editor.putInt("INT_KEY ",0);
Editor.putBoolean("BOOLEAN_KEY ",true);
editor . commit();
//Returns the value of STRING_KEY.
Log.d("SP ",sp.getString("STRING_KEY "," none "));
//If NOT_EXIST does not exist, the return value is "None".
Log.d("SP ",sp.getString("NOT_EXIST "," none "));
}
}
The second type: file stores data.
As for file storage, Activity provides the openFileOutput () method, which can be used to output data to a file. The specific implementation process is the same as saving data to a file in J2SE environment.
Files can be used to store a large amount of data, such as text, pictures, audio and so on.
Default location: /data/data/ > /files/***. ***。
Code example:
Public void save(){
Try {
File output stream out stream = this.openfile output ("a.txt", context. Mode _ world _ readable);
outStream.write(text.getText()。 toString()。 getBytes());
out stream . close();
Toast.makeText(MyActivity.this, "save", Toast. Length _ length). show();
} catch(file not found exception e){
Return;
}
catch (IOException e){
Return;
}
}
The third type: SQLite database stores data
SQLite is a lightweight embedded database engine, which supports SQL language, has good performance and occupies less memory. In addition, it is open source and anyone can use it. Many open source projects (Mozilla, PHP, Python) use SQLite.
SQLite consists of the following components: SQL compiler, kernel, backend and accessories.
By using virtual machine and virtual database engine (VDBE), SQLite makes it more convenient to debug, modify and extend the SQLite kernel.
Example of reading a file:
Public void load(){
Try {
file inputstream inStream = this . openfile input(" a . txt ");
ByteArrayOutputStream = new ByteArrayOutputStream();
Byte[]buffer = new byte [1024];
int length =- 1;
while((length = instream . read(buffer))! =- 1) {
Stream.write (buffer, 0, length);
}
stream . close();
instream . close();
text . settext(stream . tostring());
Toast.makeText(MyActivity.this, "loaded", Toast. Length _ length). show();
} catch(file not found exception e){
e . printstacktrace();
}
catch (IOException e){
Return;
}
}
The fourth method of using ContentProvider to store data, ContentProvider actually stores data through a database, so I won't describe it in detail here.
The fifth network stores data, which means that the data is stored in the server. On android, you only need to send a request through httpclient to get data from the server.
Fire station, post horse, telegraph, telephone, etc. The tools for human to store information are: ox bones and bamboo.