Android management contacts (including add, query, modify and delete; and differences between different versions)

Source: Internet
Author: User

You can easily manage contact information through interfaces provided by the Android system.

1. Add

1. Add a contact
Code on 1.6:

String peoplename = "name"; contentvalues personvalues = new contentvalues (); // namepersonvalues. put (contacts. people. name, peoplename);/* starred 0 = contacts, 1 = favorites */personvalues. put (contacts. people. starred, 0); Uri newpersonuri = contacts. people. createpersoninmycontactsgroup (getcontentresolver (), personvalues );

2.1, the following code is required to add:

String peoplename = "name"; contentvalues personvalues = new contentvalues (); // namepersonvalues. put (contacts. people. name, peoplename); newpersonuri = getcontentresolver (). insert (people. content_uri, personvalues );

 

2. Add a phone number
You can add different types of phone numbers based on the type.

Supported types in 1.6 are: type_custom, type_fax_home, type_fax_work, type_home, type_mobile, type_other, type_pager,

Type_wo

Uri uri = NULL; contentvalues values = new contentvalues (); // Add phone numberstring Tel = "86-21-65432100"; if (! Apputils. isempty (TEL) {values. clear (); uri = Uri. withappendedpath (newpersonuri, contacts. people. phones. content_directory); values. put (contacts. phones. type, contacts. phones. type_home); values. put (contacts. phones. number, TEL); getcontentresolver (). insert (Uri, values );}

 

3. Add an email address
You can add different contact methods by changing the value of kind.
1.6 Support for kind_email, kind_im, kind_organization, kind_phone, kind_postal

// Add email addressstring email = "abc@com.cn"; if (! Apputils. isempty (email) {values. clear (); uri = Uri. withappendedpath (newpersonuri, contacts. people. contactmethods. content_directory); values. put (contacts. contactmethods. kind, contacts. kind_email); values. put (contacts. contactmethods. data, email); values. put (contacts. contactmethods. type, contacts. contactmethods. type_work); getcontentresolver (). insert (Uri, values );}

 

4. Add a company and Position

// Add Company Name & titlestring Company = "Google? "; String position =" CEO !!! "; If (! Apputils. isempty (company) |! Apputils. isempty (position) {values. Clear (); uri = URI. withappendedpath (newpersonuri, contacts. Organizations. content_directory); // company nameif (! Apputils. isempty (company) {values. Put (contacts. Organizations. Company, companynametext);} // positionif (! Apputils. isempty (position) {values. put (contacts. organizations. title, positionnametext);} values. put (contacts. organizations. type, contacts. organizations. type_work); getcontentresolver (). insert (Uri, values );}

 

Ii. Query

To implement the query function, it may require some SQL basics.

1. query person names

// The contents want to getstring projection [] = new string [] {people. _ id, people. name}; // the name to be foundstring name = "Find me"; // start searchcursor cur = getcontentresolver (). query (people. content_uri, projection, // select sentencepeople. name + "=? ", // Where sentencenew string [] {name}, // Where valuespeople. name); // order byif (cur. movetofirst () {// get the resultsdo {string id = cur. getstring (cur. getcolumnindex (people. _ id); string name = cur. getstring (cur. getcolumnindex (people. name);} while (cur. movetonext ();} // close while finisconditioned (cur! = NULL) {cur. Close ();}

You can obtain different content by modifying the content of the project.

 

2. query the phone number

If you want to obtain the phone number, you can change it to (the ID is obtained by the above Code)

String phoneprojection [] = new string [] {contacts. phones. person_id, contacts. phones. number}; cursor phonecursor = getcontentresolver (). query (contacts. phones. content_uri, phoneprojection, // selectcontacts. phones. person_id + "=" + id, // Where (another style) null, contacts. phones. default_sort_order); // orderif (phonecursor. movetofirst () {// get the resultsdo {string phone = phonecursor. getstrin G (phonecursor. getcolumnindex (contacts. Phones. Number);} while (phonecursor. movetonext ();} // close while finisconditioned (phonecursor! = NULL) {phonecursor. Close ();}

3. query email addresses

If you want to get an email address, it is a little troublesome.

String emailprojection [] = new string [] {contacts. phones. person_id, contacts. contactmethods. kind, contacts. contactmethods. data}; cursor emailcursor = getcontentresolver (). query (contacts. contactmethods. content_uri, emailprojection, // selectcontacts. contactmethods. person_id + "=" + id, // wherenull, contacts. contactmethods. default_sort_order); // orderif (emailcursor. movetofirst () {do {int kind = Emailcursor. getint (emailcursor. getcolumnindex (contacts. contactmethods. kind); If (contacts. kind_email = kind) {email = emailcursor. getstring (emailcursor. getcolumnindex (contacts. contactmethods. data) ;}} while (emailcursor. movetonext ();} // close while finisconditioned (emailcursor! = NULL) {emailcursor. Close ();}

 

3. Modify

 

Iv. Delete

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.