In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to achieve data storage in Android development. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
In Android, there are several ways to achieve data persistence:
Shared Preferences: shared parameter form, a way to save data in the form of Key-Value key-value pairs, Android built-in, general application configuration information, it is recommended to use this way to save.
Internal Storage: use the memory that comes with the Android device to store data.
External Storage: uses an external storage device to store data, which usually refers to Sdcard.
SQLite Databases: stores structured data in a SQLite database.
SharedPreferences
It is also a lightweight way of data storage, its essence is based on XML files to store key-value key-value pairs of data, usually used to store some simple configuration information. Its storage location is in the / data/data//shared_prefs directory. The SharedPreferences object itself can only obtain data and does not support storage and modification, which is realized through the Editor object. The steps to implement SharedPreferences storage are as follows:
First, obtain the SharedPreferences object according to Context
Second, use the edit () method to obtain the Editor object.
Third, key-value key-value pairs are stored through Editor objects.
Fourth, submit the data through the commit () method.
Assignment:
PutBoolean (KEY_SHOW_DIALOG_AT_START, false)
Value:
GetBoolean (KEY_SHOW_DIALOG_AT_START,false)
SharedPreferences case study:
Add a check box
Define three variables
Private CheckBox cbSent
Private SharedPreferences sp
Privatestaticfinal String KEY_SHOW_DIALOG_AT_START = "showDialog"
Add to onCreate
Sp = getSharedPreferences ("mysp", Context.MODE_PRIVATE)
Cb = (CheckBox) findViewById (R.id.cb)
Cb.setOnCheckedChangeListener (new CompoundButton.OnCheckedChangeListener () {
@ Override
Publicvoid onCheckedChanged (CompoundButton buttonView, boolean isChecked) {
Editor e = sp.edit ()
E.putBoolean (KEY_SHOW_DIALOG_AT_START, isChecked)
E.commit ()
}
});
Cb.setChecked (sp.getBoolean (KEY_SHOW_DIALOG_AT_START, false))
If (cb.isChecked ()) {
New AlertDialog.Builder (this) .setTitle ("title") .setMessage ("Show statement?") .setPositiveButton ("close", null) .show ()
}
Internal storage
Internal Storage
Internal storage, in Android, developers can directly use the device's internal memory to save files. By default, the and data saved in this way can only be accessed by the current program, but not in other programs, and these files will be deleted when the user uninstalls the program.
Using internal storage to save data is basically to get the output stream of a file, then write the information to be written into the output stream in the way of write (), and finally close the stream, all these are the operations of the IO stream in Java. The specific steps are as follows:
Use the Context.openFileOutput () method to get a FileOutputStream object.
Write the content to be written to the FileOutputStream object through the write () method.
Finally, use close () to close the stream.
The Context.openFileOutput () method described above has two overloaded functions, and their signatures are:
FileOutputStream openFileOutput (Stringname): opens the name file in MODE_PRIVATE mode.
FileOutputStream openFileOutput (Stringname,int mode): opens the name file in mode mode.
In the second overloaded function above, mode is a data of type int, which generally uses the constant parameters set in the Context object, including the following:
MODE_APPEND: opens a file as an append, and everything written in this mode is appended to the original content.
MODE_PRIVATE: private mode (default), recreates and replaces the original file if it already exists, or creates it directly if it doesn't exist.
MODE_WORLD_READABLE: open the file as read-only.
MODE_WORLD_WRITEABLE: open the file in a write-only manner.
There are several other methods to pay special attention to. These methods provide better support for file relationships. In combination with the methods described above, you can perform routine CRUD operations (adding, deleting, modifying and querying) on the file data, as follows:
File getFIlesDir (): gets the absolute path to the file system.
Boolean deleteFile (String name): deletes a file with the specified file name name.
String [] fileList (): all file names under the internal storage path of the current application.
Internal Storage case study:
1) add edit and command buttons
2) read data
Privatevoid readSavedText () {
Try {
InputStream is = openFileInput ("data")
Byte [] bytes = newbyte [is.available ()]
Is.read (bytes)
Is.close ()
String str = new String (bytes, "utf-8")
Et.setText (str)
} catch (FileNotFoundException e) {
E.printStackTrace ()
} catch (IOException e) {
E.printStackTrace ()
}
}
3. Save data
Privatevoid saveCurrentText () {
Try {
OutputStream os = openFileOutput ("data", Context.MODE_PRIVATE)
Os.write (et.getText (). ToString (). GetBytes ("utf-8"))
Os.flush ()
Os.close ()
Toast.makeText (this, "saved successfully", Toast.LENGTH_SHORT) .show ()
Return
} catch (FileNotFoundException e) {
E.printStackTrace ()
} catch (UnsupportedEncodingException e) {
E.printStackTrace ()
} catch (IOException e) {
E.printStackTrace ()
}
Toast.makeText (this, "Save failed", Toast.LENGTH_SHORT) .show ()
}
4) start reading data and definition saving
Et = (EditText) findViewById (R.id.et)
BtnSave = (Button) findViewById (R.id.btnSave)
ReadSavedText ()
BtnSave.setOnClickListener (new View.OnClickListener () {
@ Override
Publicvoid onClick (View v) {
SaveCurrentText ()
}
});
This is the end of the article on "how to achieve data storage in Android development". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 246
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.