Directory (?) [+]
When an application is installed in Android, we generate a lot of data in the process of using the app, and the app has its own data, so how do we store the data?
How data is stored
There are 5 ways to store your Android data:
1. sharedpreferences Storage Data
Sharedpreferences data storage, also known as XML storage. This is the data stored under the "data/data/Package name/share_prefs" path to the XML file.
Related connections: "Data storage--sharedpreferences storage data in Android"
2. File storage Data
Divided into internal storage and external storage. Internal storage is an application that uses Android to allocate memory space for itself, and the data is stored in the appropriate file under the "/data/data/Package name/files" path. The external storage is the memory that uses the mobile phone sdcard (this sdcard is not what we often say that can disassemble the replacement SD card, that SD card we call the expansion card), use this part of memory to declare the corresponding permission.
RELATED links: "Data storage in Android-file storage Data"
3. SQLite Database stores Data
Using a database for storage, this general amount of data is relatively large.
Related connection: "Data storage--sqlite Database storage data" in Android
4. Storing data using ContentProvider
This looks familiar, ContentProvider is also one of the four components of Android. ContentProvider is generally a third party to provide data storage, to our phone contacts, photos, music, etc...
Related connections: "Data storage--contentprovider storage data in Android"
5. Networked Storage Data
This is to upload the data to the network for storage.
Here's what we're going to do today, using ContentProvider to store data.
ContentProvider Storing Data
The ContentProvider content provider is used primarily for sharing data between different applications. For example, we develop an application that we cannot use only our own data, but also the data of other applications, such as contacts in the phone, pictures, music, etc. are used to the most. We use sharedpreferences, file storage, and database SQLite to store data from within the application, to achieve data sharing between different applications will be used to contentprovider.
There are two ways to use ContentProvider: one is to use an existing content provider to read and manipulate the data in the corresponding program, and the other is to create your own content provider to provide external access to our application.
Here we only explain the use of the existing content provider to read and manipulate the corresponding program data.
Use of Contentresolver
To access content in the content provider we need to use the Contentresolver class.
Contentresolver gives us the "add" Insert (URI URL, contentvalues values), "delete" delete (URI URL, String where, string[] Selectionargs) , "Change" update (URI Uri, contentvalues values, String where, string[] selectionargs), "check" query (URI URI, string[] projection, St Ring selection, string[] Selectionargs, String SortOrder) method to the contents of the content provider to operate, is not very familiar with, Ah, and sqlitedatabase in the deletion and change of the operation is the same, We will not describe this in detail here.
use of URIs
In Sqlitedatabase, the operation of the database is performed by accepting the table name of the database, while the table name is not accepted in Contentresolver and the URI object is accepted. Specifies the content "location" of the operation through a URI.
A URI consists of two parts: a permission and a path. Permissions are differentiated for different applications, typically with the application's package name, and paths are differentiated for different tables in the same application. For example, we have an application with the package name "Com.example.ontentproviderdemo", and the application has a table "table", the URI is: "content:// Com.example.ontentproviderdemo/table ". We can use the parse () method to parse it into a URI.
1 Uri uri = Uri.parse.("content://com.example.ontentproviderdemo/table");
Query Contacts
Let's take a look at the contents of the Address book as an example to see the use of ContentProvider. Access contact data from your contacts via the ContentProvider content provider provided by Android.
1. To read a contact is required permission, first add the permissions in Androidmanifext:
12 <uses-permission android:name= "Android.permission.READ_CONTACTS"/>
2. Create a Contentresolver object to manipulate the ContentProvider.
3. Create a URI object that specifies access to the address book. Here we use the URI that has been resolved by Android: ContactsContract.CommonDataKinds.Phone.CONTENT_URI
4. Find contact content Use the Contentresolver query () method to return a cursor object.
1Uri uri =ContactsContract.CommonDataKinds.Phone.CONTENT_URI;2cursor cursor = contentresolver.query (URI,NewString[]{contactscontract.commondatakinds.phone.display_name, ContactsContract.CommonDataKinds.Phone.NUMBER},NULL,NULL,NULL);3 Cursor.movetofirst ();4 while(!Cursor.isafterlast ()) {5String name =cursor.getstring (Cursor.getcolumnindex (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));6String num =cursor.getstring (Cursor.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER));7LOG.D ("Data", "Contact person Name:" + name + ", Phone:" +num);8 Cursor.movetonext ();9}
Address Book content:
Read Result:
Data storage in Android (iv)--contentprovider storage data