Android Study Notes (4th 7): Content Provider initial discussion and Android contact information

Source: Internet
Author: User

Content Provider

In data processing, Android usually uses the Content Provider method. Content Provider uses the Uri instance as the data encapsulation of the handle to conveniently add, delete, modify, and query data. Android does not provide data storage shared by all applications. Using content Provider, Android provides simple and convenient interfaces to maintain and obtain data, and cross-application data access. In short, Android obtains information from data encapsulation through content Provider.

Content provider uses Uri to locate information. Take"Content ://It indicates that this is a content URI. For example, "content: // contstans/5", "content: // constans" is called base URI, which is equivalent to the namespace of data. Its structure can be more complex and has multiple layers. In this example, "5" is the ID of a specific instance.

Generally, there are four data storage methods for Android:

1. Preferences. For more information, see Preference usage. Generally, key-value pairs (name-value pair) are used ).
2. Files: files are stored on mobile devices or peripherals. By default, files can only be accessed by created applications.
3. database (RDBMS): SQLite method. For more information, see the usage of SQLite. It is accessed by the created application.
4. Network: Android provides APIs to remotely store data on servers.

If you want to share data in an application, use Content Provider. Regardless of the data storage method, Content Provider provides a unified interface. Data will be provided in a table-like manner, with rows and columns indicating data with different attributes, such as phone numbers and email addresses.Each record (each row) is identified by a unique _ ID field.. You can see it more intuitively in the specific code in the future.

Native providers

Android provides some native content providers, such as videos, images, and contact information in the system. For example, if the contact URI is "content: // com. Android. Contacts/contacts", the native providers are generally defined. For example, the contact is contactscontract. Contacts. content_uri. Because different versions of the system may be different, we should try our best to use the definition of the system.

In Android. in the provider package, you can view native providers, such as alarmclock, browser, calendarcontract, calllog, and contactscontract (including contacts, groups, and phonelookup) mediastore (Audio "albums, artists, genres, playlists", files, images, and video) and setting.

Read information through content providers

We use native provider contacts to see how to read information. The data structure of contacts is complex and will be described in detail later. Here we will try to simplify it as much as possible. First, we will have an intuitive understanding of the content provider. The core is throughManagedQuery ()To obtain the cursor. Cursor returns a two-dimensional table with rows and columns. rows are specific elements and columns are specific attributes of the element. The entire method is very similar to SQLite.

// You can easily map the content of cursor to listactivity. For more information, see Android Study Notes (): SQLite, listview, and contextmenu.
Public class chapter26test1 extendsListActivity{
Private string [] info = new string [] {
Contactscontract. Contacts._ ID,
// For the data table returned by the Content Provider, note that the unique identifier _ ID must be read. Otherwise, an error is easily reported.
ContactsContract. Contacts. DISPLAY_NAME,
ContactsContract. Contacts. HAS_PHONE_NUMBER
};
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
/*ManagedQuery () reads data from the Content Provider
* The 1st parameter indicates the URI,
* The 2nd parameter indicates the information to be read ,;
* The 3rd parameters are restrictions, similar to WHERE in SQL;
* 4th parameters and 3rd parameters are used together. Specifically, "?" in the third parameter is supported. Specific reasons;
* The 5th parameters are similar to order by in SQL */

Cursor contactCursor =ManagedQuery (ContactsContract. Contacts. CONTENT_URI,Info, Null);
ListAdapter adapter = new SimpleCursorAdapter (this,
R. layout. chapter_22_test1,ContactCursor,
New String [] {contactscontract. Contacts. display_name, contactscontract. Contacts. has_phone_number },
New int [] {R. Id. c22_name, R. Id. c22_gravity });
Setlistadapter (adapter); // This example uses the XML
}
}

About the structure and reading of Android contact information

This is the contact organization diagram provided by Android. It is divided into three layers.

The first layer, contact, that is, contactcontract. Contacts, is the Integrated contact information.

Layer 2 rawconact, I .e. contactcontract. rawcontact records the information of the contact from a certain source of information, such as locally input from Google, exported from Microsoft Exchange, or from a certain social network website. Each rawcontact record information comes from the same source of information.

Layer 3: data, that is, contactcontract. data is the storage of specific information, such as recording the contact name, email information, home phone number, mobile phone number information, etc. Each data contains a specific information. The MIME type in data indicates the type of the stored information. It is defined in contactcontract. commondatakinds. Each data has a DATA1-DATA15 field to store information, the meaning of each field, can also be found in commondatakinds.

Next, we read the contact information layer by layer to see this structure more clearly. To read the detailed information, the key to the content privider is to locate the URI layer by layer to obtain the information. For clarity, we will write the entire path.

The first layer of information will return multiple Contacts in the contact list. The Uri is ContactsContract. Contacts. CONTENT_URI

Private string [] info_1 = new string [] {
Contactscontract. Contacts._ ID, // This is the unique ID of each row, that is, each contact
Contactscontract. Contacts. display_name,
};
Cursor cursor = managedquery (ContactsContract. Contacts. CONTENT_URI,INFO_1, Null );

Cursor Is a table. Each row represents a contact. The columns to read are defined in the second parameter string. You can select the columns, which can be found in the column in contactscontract. Contacts in the android reference. The most critical column information is _ id, which is the unique identifier of a row, that is, each contact. In addition, the information of each contact is integrated based on its rawcontact. Therefore, we can find the contact name contactscontract. Contacts. display_name.

Using contacts. _ ID, we can retrieve the second-level information.

Layer 2 Information. Multiple RawContacts of a contact are returned.

Similarly, you can view the columns in the returned table in ContactsContract. RawContacts. Select the following information. In addition, if you want to view the information source, such as com. google, you can view the information in the RawContacts. ACCOUNT_TYPE and RawContacts. ACCOUNT_NAME columns.

Private String []INFO_2= {
ContactsContract.RawContacts. _ ID, // This is the unique identifier of RawContact.
ContactsContract.RawContacts. CONTACT_ID, // This is the Contact. _ ID associated with the RawContact.
};

By querying all RawContacts and conditional retrieval, the contact ID is required to be a specified contact. In the following example, the conditions of param3 AND param4 are given in managedQuery. Note that parameter 3 can be expressed by logical conformances such as AND if we have multiple conditions.

Cursor cursor = managedQuery (ContactsContract. RawContacts. CONTENT_URI,
INFO_2,
RawContacts. CONTACT_ID + "=? ",
New String [] {String. valueOf (contactId )},
Null );

Through rawcontacts. _ ID, we can retrieve the information of each data in rawcontacts.

Layer 3 Information: returns the Data information of a RawContact.

For information about each column to be queried, see contactscontract. Data.

Private string info_3 [] = {
Contactscontract. Data. _ id, // This is the unique identifier of the data information block.
Contactscontract. Data. contact_id, // contact_id associated with the first level of information
Contactscontract. Data. raw_contact_id, // raw_idcontact_id associated with Layer 2 Information
Contactscontract. Data. mimetype, // For details, see contactscontract. commondatakinds.
Contactscontract. Data. data1, // usually read data1, in the specific contactscontract. commondatakinds. Phone/email to find the meaning of the DATA1-DATA15
Contactscontract. Data. data2,
... ...
ContactsContract. Data. DATA15,
};

There are two reading methods.

Method 1: specify the specific Uri:

Uri rawContactUri =ContentUris. withAppendedId (ContactsContract. RawContacts. CONTENT_URI, rawId );
Uri dataUri =Uri. withAppendedPath (rawContactUri, ContactsContract. RawContacts. Data. CONTENT_DIRECTORY );
Cursor cursor = managedQuery (dataUri, INFO_3, null );

Method 2: Use RawContacts_ID or Contact_IDData. CONTENT_URIThe specific method is RawContacts retrieval.

Cursor cursor = managedQuery (ContactsContract. Data. CONTENT_URI,
INFO_3,
Contactscontract. Data. raw_contact_id + "=? ",
New String [] {string. valueof (rawid )},
Null );

Summarize the architecture diagram. In fact, Android contacts provide multiple URI query methods. For details, refer to the Android reference.

Related Links: My Andriod development articles

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.