Android data storage,

Source: Internet
Author: User

Android data storage,

 

When the app is running, you need to save some user information. For example, the logon status and account information. Android provides multiple methods to store user data.

 

1. key-value storage, implemented through SharedPreferences

SharedPreferences sp = getSharedPreferences ("test", Context. MODE_PRIVATE );

Sp. edit (). putString ("str", "hello"). commit ();

Log. e (TAG, "save1:" + sp. getString ("str", null ));

2. Save the file and write the data into the internal memory.

File file = getFilesDir ();

Log. e (TAG, "save2:" + file. getAbsolutePath ());

 

File f2 = new File (file, "test. log ");

Try {

F2.createNewFile ();

// Write data into the file

Log. e (TAG, "save2:" + f2.exists ());

} Catch (IOException e ){

E. printStackTrace ();

}

3. Save the database and read and write structured data through the SQLite Database

Create a database through openOrCreateDatabase or inherit from SQLiteOpenHelper

 

4. Save the network and save the data on the server through the http protocol

 

URL url = new URL ("http://www.android.com /");
HttpURLConnection urlConnection = (HttpURLConnection) url. openConnection ();
Try {
InputStream in = new BufferedInputStream (urlConnection. getInputStream ());
ReadStream (in );
} Finally {
UrlConnection. disconnect ();
}

Instance download: https://files.cnblogs.com/files/bruce2020/TestSQLite.zip

Related Article

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.