In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to save data in Android". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to save data in Android" can help you solve the problem.
Preferences analyzes it in terms of the structure in which it stores data, which is a relatively lightweight way to store data. Similar to our commonly used ini file to save software initialization settings, it is also used to store simple parameter settings on the Android platform. For example, you can use it to save the last user's changes or custom parameter settings, and keep the original settings when you start the program again.
Read and write values through the Context.getSharedPreferences () method, which allows other modules in the same program to share data by setting name. If you do not need to share data with other modules, you can use the Activity.getPreferences () method to keep the data private. It is important to emphasize that Preferences data cannot be shared directly among multiple programs (not including the use of Content Providers).
Use an example to understand the actual usage:
Import android.app.Activity; import android.content.SharedPreferences; public class Calc extends Activity {public static final String PREFS_NAME = "MyPrefsFile";. . . . Override protected void onCreate (Bundle state) {super.onCreate (state);. . . . / / Restore preferences SharedPreferences settings = getSharedPreferences (PREFS_NAME, 0); boolean silent = settings.getBoolean ("silentMode", false); setSilent (silent);} @ Override protected void onStop () {super.onStop (); / / Save user preferences. We need an Editor object to / / make changes. All objects are from android.context.Context SharedPreferences settings = getSharedPreferences (PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit (); editor.putBoolean ("silentMode", mSilentMode); / / Don't forget to commit your editsthanks! Editor.commit ();}}
Files from this is the second method, you can create files to hold data in the device's own storage device or an external storage device. Also by default, files cannot be shared between different programs.
Write file: call the Context.openFileOutput () method to create a file based on the specified path and file name, which returns a FileOutputStream object.
Read the file: call the Context.openFileInput () method to return a standard Java FileInputStream object with the specified path and file name.
(note: you will not be able to apply the same path and file name to manipulate files in other programs)
In addition, before compiling the program, create a static file in res/raw/tempFile, so that you can also return an InputStream object in the program through the Resources.openRawResource (R.raw.myDataFile) method to read the contents of the file directly.
Databases includes an interface to apply SQLite databases in Android API, and the databases created by each program are private, in other words, programs cannot access each other's databases.
Create a SQLiteDatabase object in a program that contains most of the ways to interact with database, such as reading data or managing current data. You can apply the create () method of SQLiteDatabase and its subClassSQLiteOpenHelper to create a new database.
For SQLitedatabase, its powerful and convenient function provides a powerful storage function for Android. In particular, it stores some complex data structures. for example, Android creates unique data types for the address book, which contains a lot of subsets and covers most of the data types "First Name", "Last Name", "PhoneNumber" and "Photo".
Android can view the contents of the tables in the specified database through Sqlite3 database tool, and directly run the SQL command to quickly and easily directly manipulate SQLite database.
The address where the database is stored in the device is / data/data/package_name/databases
Network acquires and stores data resources through the network, and this method requires devices to maintain a network connection, so there are some relative limitations. Two classes for related operations are listed below:
This is the end of java.net.* android.net.* 's introduction to "how to save data in Android". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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: 0
*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.