Android Contacts (Android contacts read)-content provider

Source: Internet
Author: User

Content Provider

In data processing, Android usually uses the content provider method. Content provider uses the URI instance as the handle of the data encapsulation, it is convenient to access the data to increase, delete, change, check the operation. Android does not provide a shared data store for all applications, with content Provider, which provides a simple and convenient interface to maintain and retrieve data, as well as to achieve data access across applications. Simply put, Android uses content provider to get information from the package of data.

Content provider uses URIs to locate information. Start with "content://" to indicate that this is a content URI. For example "CONTENT://CONTSTANS/5", where "Content://constans" is called the base URI, equivalent to the namespace of the data, its structure can be more complex, can have a multilayer structure. The "5" in the example is the identity of the specific instance.

In general, there are 4 ways to store your Android data:

1, Preferences, see the use of preference, is usually a key-value pair (Name-value pair).
2. Files: stored on mobile devices or peripherals, the default can only be accessed by the app created.
3. Database (RDBMS): SQLite mode, see the use of SQLite, accessed by the app created.
4. Network: Android provides APIs to remotely store data on the server.

If we need to share in the app, use content Provider. Content provider provides a unified interface, regardless of the data-specific storage method. The data will be provided in the form of a class table, with rows and columns, and columns representing data of different attributes, such as phone numbers, e-mail addresses, etc. each record (each line) has a unique _id field to identify . It's more intuitive to see later in the specific code.

Native providers

Android provides native content provider, video, images, and contact information from the system, among other things. For example, the URI of the contact is "content://com.android.contacts/contacts", and the native providers is generally defined, such as contact person ContactsContract.Contacts.CONTENT _uri. Since different versions of the system may differ, we should use the definition of the system whenever possible.

In Android.provider's package, you can view native provider, such as AlarmClock, Browser, Calendarcontract, Calllog, Contactscontract (including Contacts,groups,phonelookup, etc.), Mediastore (Audio "Albums, Artists, genres, playlists", Files, Images, Video) and setting.

Reading information via content providers

We use the native provider contact to see how to read the information, the contact data structure is more complex, the following will be described in detail, here we will be as simple as possible, first on the content provider have an intuitive understanding. The core is to get the cursor through managedquery () . The cursor is actually returned as a two-dimensional table with rows and columns, rows are concrete elements, and columns are specific attributes of that element. The whole way is very similar to SQLite.

It is convenient to map the cursor content to listactivity, see Android Learning Note (42): Sqlite,listview,contextmenu
public class Chapter26test1 extendslistactivity{
Private string[] INFO = new string[]{
Contactscontract.contacts._id,//For the content provider return data table, note that the unique identification _id is usually required to read, otherwise it is easy to error
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER
};
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);

Cursor Contactcursor =Managedquery (ContactsContract.Contacts.CONTENT_URI,INFO, Null,null,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); Stealing lazy, this example takes advantage of the XML in the SQLite example of the Android learning Note (42)
}
}

Talk about the organizational structure and reading of Android contact information

This is the contact org chart given by Android. Divided into three layers.

The first layer, contact, or contactcontract.contacts, is the integrated contacts information.

The second layer, rawconact, or contactcontract.rawcontact, records information from a source, such as locally entered, from Google, from Microsoft Exchange, or from a social networking site. Each rawcontact records information from the same information source.

The third layer, data, i.e., Contactcontract.data, is a specific information store, such as record contact name, email information, home phone, mobile phone information and so on, each of the data is stored a specific information. The MIME type in data describes the type of information stored. Specifically defined in the contactcontract.commondatakinds. Each data has DATA1-DATA15 fields to store information, the meaning that each field represents, or it can be found in commondatakinds.

Below, we read the contact information one layer at a level, and we will see the structure more clearly. To read the details, the key to content Privider is that the URI is positioned on a layer-by-level basis to obtain information. For clarity, we write the entire path.

The first layer of information will return multiple Contacts,uri in the contact list as ContactsContract.Contacts.CONTENT_URI

Private string[] info_1= new string[]{
Contactscontract.contacts. _id,//This is the unique ID identifier of each row, that is, each contact.
ContactsContract.Contacts.DISPLAY_NAME,
};
cursor cursor = managedquery (ContactsContract.Contacts.CONTENT_URI,info_1, null,null,null);

The cursor is a tabular form, and each row represents a contact. Which columns to read are defined in the second parameter, string[]. You can select those columns, which can be found in the column in Contactscontract.contacts in Android reference. The most critical column information is _id, which is the row, which is the unique identity of each contact. In addition, each contact's information will be integrated according to the rawcontact, so we can find the contact name ContactsContract.Contacts.DISPLAY_NAME and so on.

Using CONTACTS._ID, we can retrieve the second level of information.

The second layer of information will return multiple rawcontacts of a contact.

Similarly, the columns in the returned table can be viewed in contactscontract.rawcontacts. We select the following information, and if you want to see the source of the information, such as from Com.google, you can view it through the Rawcontacts.account_type and rawcontacts.account_name two columns.

Private string[] info_2 = {
Contactscontract. rawcontacts._id,//This is the unique identity of Rawcontact
Contactscontract. rawcontacts.contact_id,//This is the rawcontact associated contact._id
};

By querying all the rawcontacts, the contact's ID is required to be a specified contact, through conditional retrieval. In the following example, the conditions of param3 and param4 are given in managedquery, note that parameter 3, if we have multiple conditions, can be expressed with and and other logical conformance.

cursor cursor = managedquery (ContactsContract.RawContacts.CONTENT_URI,
Info_2,
rawcontacts.contact_id + "=?",
new String[]{string.valueof (contactId)},
NULL);

Through rawcontacts._id, we can search down the information content of the data below rawcontacts.

The third layer of information: the information that will return the data of a rawcontact

The information for each column can be found in Contactscontract.data's introduction.

Private String info_3[] = {
CONTACTSCONTRACT.DATA._ID,//This is the unique identifier of the data information block
ContactsContract.Data.CONTACT_ID,//contact_id of the first layer of information associated
ContactsContract.Data.RAW_CONTACT_ID,//raw_idcontact_id of the second-tier information
ContactsContract.Data.MIMETYPE,//specific meaning can be found contactscontract.commondatakinds
ContactsContract.Data.DATA1,//usually read DATA1 can, in the specific contactscontract.commondatakinds.phone/email and so find data1-data15 meaning
ContactsContract.Data.DATA2,
... ...
CONTACTSCONTRACT.DATA.DATA15,
};

There are two ways to read.

Mode one: Specify a 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, NULL, NULL);

Mode two: Another is to use rawcontacts_id or contact_id, through Data.content_uri, the specific way of class 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's contacts provide a variety of URI query methods, specifically to participate in Android reference.

Android Contacts (Android contacts read)-content provider

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.