Android Data storage

Source: Internet
Author: User

Synchronized (synchronous): When it is used to decorate a method or a block of code, it is guaranteed that at most one thread at a time executes the code.
First, when two concurrent threads access the same object in the synchronized (this) synchronization code block, only one thread can be executed within a single time. The other thread must wait for the current thread to finish executing the block before it can execute the code block.
Second, however, when a thread accesses one synchronized (this) of an object to synchronize a block of code, another thread can still access the non-synchronized (this) synchronous code block in the object.
Third, in particular, when a thread accesses one synchronized (this) of an object to synchronize a block of code, other threads will block access to all other synchronized (this) synchronization blocks in object.
The third example also applies to other synchronous blocks of code. That is, when a thread accesses a synchronized (this) of object to synchronize a block of code, it obtains the object lock of the objects. As a result, other threads are temporarily blocking access to all of the synchronization code portions of the object.
The above rules apply to other object locks as well.

Production random number: float f = math.random (),//{o,1)
Random random = new random (), int i = random.nextint (100),//[0,100) between
light weight Level storage Tool:
Sharedpreferences: Saved in the phone, the specific path is data/data/your package name/shared_prefs/saved file name. XML
holds the Key-value key-value pair data stored based on the XML file
Sharedpreferences = Context (Activity). Getsharedpreferences (String name, int mode);//If the file does not exist, create a new

The Sharedpreferences object that is obtained by invoking the Getsharedpreferences () method of the context object can be shared by other components under the same application
calling the Activity object's getpreferences () The Sharedpreferences object obtained by the method can only be used in the activity, which is the difference between the activity and the context.
Mode:
Mode_private: Files can only be created by the program to access the
Mode_append: Check the file, the file exists at the end of the file is added, does not exist the file is created and added inside the
Mode_world_ Readable: All programs can access the file but cannot change the
mode_world_writeable: All programs can change the contents of the file

Editor edit = Sharedpreferences.edit (); Edit.putint (key, value);..
Edit.commit ();//edit.appiy (); There are two ways to commit
:
Commit (synchronous), apply (asynchronous).
Difference: (1) commit is to save the data directly to the hard disk, and apply is to upload the data into memory, and then asynchronous to disk,
If there are multiple commit concurrent commit data, can only queue one commit, which greatly reduces the efficiency;
(2) Commit returns Information, but apply does not give any hint, even if the save fails.

SQLite: Lightweight Database
String dbName = "test.db";
Sqliteopenhelper helper = new Sqliteopenhelper (context, DbName, Cursorfactory, version) {
@Override
public void OnCreate (Sqlitedatabase db) {
Db.execsql ("CREATE table if not exists" + tablename+ "(Id integer primary key, Record text, Orderprice integer, Country Tex t) ");
}

@Override
public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {
Db.execsql ("drop table if exists" + tableName);
OnCreate (DB);
}
};
Sqlitedatabase database = Helper.getwritabledatabase ();
Database.begintransaction ();
Database operation additions and deletions change
Database.endtransaction ();
Database.close ();
Ormlite Frame:
First, Bean class, data object instantiation
Second, write helper class, inherit the Ormlitesqlitehelper class (functions completed in this class are: CREATE Database DB, new datasheet table, and build a table when the version is updated)
Third, the DAO class, can be used basedao<t> Change the data object and delete the operation

Android Data storage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.