Android Action Contact Android Development tutorial

Source: Internet
Author: User

The contacts in the Android system are also available through ContentProvider, where we get all the contacts, get contacts by phone number, add contacts, add contacts using transactions.

Get all Contacts

1. The contacts in the Android system are also available through the ContentProvider for external data.

2. The database path is:/data/data/com.android.providers.contacts/database/contacts2.db

3. There are 3 tables that we need to focus on

Raw_contacts: The Contact ID is saved

Data: And Raw_contacts is a many-to-one relationship that preserves the contacts ' individual

Mimetypes: For data type

4. The authorites of provider is com.android.contacts

5. The path to query the Raw_contacts table is: Contacts

6. The path to query the data table is: Contacts/#/data

This path is a connection query, the query "mimetype" field can be queried according to "MIMETYPE_ID" to the data in the Mimetypes table

7. Query Raw_contacts to get the ID of each contact, use the ID to query the data table, according to mimetype classification data

Example:

Find all Contacts

public void Testgetall () {

Contentresolver resolver = GetContext (). Getcontentresolver ();

Uri uri = uri.parse ("content://com.android.contacts/contacts");

Cursor idcursor = Resolver.query (URI, new string["{" _id "}, NULL, NULL, NULL);

while (Idcursor.movetonext ()) {

Gets the ID in the Raw_contacts table

int id = idcursor.getint (0);

Querying data in the database table based on the obtained ID

URI = Uri.parse ("content://com.android.contacts/contacts/" + ID + "/data");

Cursor datacursor = Resolver.query (URI, new string[] {"Data1", "mimetype"}, NULL, NULL, NULL);

StringBuilder sb = new StringBuilder ();

Sb.append ("id=" + ID);

Query in the Contact table

while (Datacursor.movetonext ()) {

String data = datacursor.getstring (0);

String type = datacursor.getstring (1);

if ("Vnd.android.cursor.item/name". Equals (Type))

Sb.append (", name=" + data);

else if ("Vnd.android.cursor.item/phone_v2". Equals (Type))

Sb.append (", phone=" + data);

else if ("Vnd.android.cursor.item/email_v2". Equals (Type))

Sb.append (", email=" + data);

}

System.out.println (SB);

}

}

Get Contacts by phone number

1. The system provides the ability to obtain data from a phone number, the path is: data/phones/filter/*

2. Replace the "*" section with the phone number to find the required data, get "display_name" to get the contact display name.

Example:

Query contact name by phone number

public void Testgetname () {

Contentresolver resolver = GetContext (). Getcontentresolver ();

Uri uri = uri.parse ("content://com.android.contacts/data/phones/filter/1111");

Cursor C = resolver.query (URI, new string[] {"Display_name"}, NULL, NULL, and NULL);

while (C.movetonext ()) {

System.out.println (c.getstring (0));

}

}

Add a Contact

1. Insert the ID into the Raw_contacts table first, the path is: raw_contacts

2. After obtaining the ID and then inserting the data into the table, the path is:

Example:

Add a Contact

ublic void Testinsert () {

Contentresolver resolver = GetContext (). Getcontentresolver ();

Uri uri = uri.parse ("content://com.android.contacts/raw_contacts");

Contentvalues values = new Contentvalues ();

Inserts a record that is all NULL except the ID for raw_contacts, and the ID is automatically generated

Long id = Contenturis.parseid (resolver.insert (URI, values));

Add a contact person's name

URI = Uri.parse ("Content://com.android.contacts/data");

Values.put ("raw_contact_id", id);

Values.put ("Data2", "FHM");

Values.put ("MimeType", "vnd.android.cursor.item/name");

Resolver.insert (URI, values);

Add a contact phone

Values.clear (); Empty the last data

Values.put ("raw_contact_id", id);

Values.put ("Data1", "18600000000");

Values.put ("Data2", "2");

Values.put ("MimeType", "vnd.android.cursor.item/phone_v2");

Resolver.insert (URI, values);

Add a Contact mailbox

Values.clear ();

Values.put ("raw_contact_id", id);

Values.put ("Data1", "[email protected]");

Values.put ("Data2", "1");

Values.put ("MimeType", "vnd.android.cursor.item/email_v2");

Resolver.insert (URI, values);

Add a contact using a transaction

1. When adding a contact is a multiple access to provider, if there is an exception in the process, there will be incomplete data situation, these operations should be placed in a transaction

2. Using the Contentresolver applybatch (String authority,arraylist<contentprovideroperation> operations) Method can execute multiple operations in a single transaction

Example:

Add a contact using a transaction

public void Testinsertbatch () throws Exception {

Contentresolver resolver = GetContext (). Getcontentresolver ();

arraylist<contentprovideroperation> operations = new arraylist<contentprovideroperation> ();

Contentprovideroperation Operation1 = contentprovideroperation//

. Newinsert (Uri.parse ("content://com.android.contacts/raw_contacts"))//

. Withvalue ("_id", NULL)//

. build ();

Operations.add (Operation1);

Contentprovideroperation Operation2 = contentprovideroperation//

. Newinsert (Uri.parse ("Content://com.android.contacts/data"))//

. Withvaluebackreference ("raw_contact_id", 0)//

. Withvalue ("Data2", "Zzh")//

. Withvalue ("MimeType", "vnd.android.cursor.item/name")//

. build ();

Operations.add (Operation2);

Contentprovideroperation Operation3 = contentprovideroperation//

. Newinsert (Uri.parse ("Content://com.android.contacts/data"))//

. Withvaluebackreference ("raw_contact_id", 0)//

. Withvalue ("Data1", "18612312312")//

. Withvalue ("Data2", "2")//

. Withvalue ("MimeType", "VND.ANDROID.CURSOR.ITEM/PHONE_V2")//

. build ();

Operations.add (Operation3);

Contentprovideroperation Operation4 = contentprovideroperation//

. Newinsert (Uri.parse ("Content://com.android.contacts/data"))//

. Withvaluebackreference ("raw_contact_id", 0)//

. Withvalue ("Data1", "www.2cto.com")//

. Withvalue ("Data2", "2")//

. Withvalue ("MimeType", "VND.ANDROID.CURSOR.ITEM/EMAIL_V2")//

. build ();

Operations.add (OPERATION4);

Batch execution of multiple operations in a transaction

Resolver.applybatch ("com.android.contacts", operations);

}

Android Action Contact Android Development tutorial

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.