ANDROID admin contact

Source: Internet
Author: User

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

Add 

Add contact
Code on 1.6

String peopleName = "name"; <br/> ContentValues personValues = new ContentValues (); <br/> // name <br/> personValues. put (Contacts. people. NAME, peopleName); <br/>/* STARRED 0 = Contacts, 1 = Favorites */<br/> personValues. put (Contacts. people. STARRED, 0); <br/> Uri newPersonUri = Contacts. people. createPersonInMyContactsGroup (<br/> getContentResolver (), personValues ); 

2.1, the following code is required before you can add

String peopleName = "name"; <br/> ContentValues personValues = new ContentValues (); <br/> // name <br/> personValues. put (Contacts. people. NAME, peopleName); <br/> newPersonUri = getContentResolver (). insert (People. CONTENT_URI, personValues ); 

Add phone number
You can add different types of phone numbers based on the TYPE.
The types supported in 1.6 are: TYPE_CUSTOM, TYPE_FAX_HOME, TYPE_FAX_WORK, TYPE_HOME, TYPE_MOBILE, TYPE_OTHER, TYPE_PAGER, and TYPE_WORK.

Uri uri = null; <br/> ContentValues values = new ContentValues (); </p> <p> // add phone number <br/> String tel = "86-21-65432100"; </p> <p> if (! AppUtils. isEmpty (tel) {<br/> values. clear (); <br/> uri = Uri. withAppendedPath (newPersonUri, Contacts. people. phones. CONTENT_DIRECTORY); <br/> values. put (Contacts. phones. TYPE, Contacts. phones. TYPE_HOME); <br/> values. put (Contacts. phones. NUMBER, tel); <br/> getContentResolver (). insert (uri, values); <br/>} 

Add 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 address <br/> String email = "abc@com.cn"; </p> <p> if (! AppUtils. isEmpty (email) {<br/> values. clear (); <br/> uri = Uri. withAppendedPath (newPersonUri, Contacts. people. contactMethods. CONTENT_DIRECTORY); <br/> values. put (Contacts. contactMethods. KIND, Contacts. KIND_EMAIL); <br/> values. put (Contacts. contactMethods. DATA, email); <br/> values. put (Contacts. contactMethods. TYPE, Contacts. contactMethods. TYPE_WORK); <br/> getContentResolver (). insert (uri, values); <br/>}< br/> 

ADD Company and Position

// Add company name & title <br/> String company = "Google? "; <Br/> String position =" CEO !!! "; </P> <p> if (! AppUtils. isEmpty (company) |! AppUtils. isEmpty (position) {<br/> values. clear (); <br/> uri = Uri. withAppendedPath (newPersonUri, Contacts. organizations. CONTENT_DIRECTORY); </p> <p> // company name <br/> if (! AppUtils. isEmpty (company) {<br/> values. put (Contacts. organizations. COMPANY, companyNameText); <br/>}</p> <p> // position <br/> if (! AppUtils. isEmpty (position) {<br/> values. put (Contacts. organizations. TITLE, positionNameText); <br/>}</p> <p> values. put (Contacts. organizations. TYPE, Contacts. organizations. TYPE_WORK); <br/> getContentResolver (). insert (uri, values); <br/>}< br/> 

Query 

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

Query person name

// The contents want to get <br/> String projection [] = new String [] {People. _ ID, People. NAME };< br/> // the name to be found <br/> String name = "find me "; <br/> // start search <br/> Cursor cur = getContentResolver (). query (People. CONTENT_URI, <br/> projection, // select sentence <br/> People. NAME + "=? ", // Where sentence <br/> new String [] {name}, // where values <br/> People. NAME); // order by </p> <p> if (cur. moveToFirst () {<br/> // get the results <br/> do {<br/> String id = cur. getString (cur. getColumnIndex (People. _ ID); <br/> String name = cur. getString (cur. getColumnIndex (People. NAME); <br/>}while (cur. moveToNext (); <br/>}</p> <p> // close while finish <br/> if (cur! = Null) {<br/> cur. close (); <br/>}

You can obtain different content by modifying the content of the project.
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 };</p> <p> Cursor phoneCursor = getContentResolver (). query (Contacts. phones. CONTENT_URI, <br/> phoneProjection, // select <br/> Contacts. phones. PERSON_ID + "=" + id, // where (another style) <br/> null, <br/> Contacts. phones. DEFAULT_SORT_ORDER); // order </p> <p> if (phoneCursor. moveToFirst () {<br/> // get the r Esults <br/> do {<br/> String phone = phoneCursor. getString (phoneCursor. getColumnIndex (Contacts. phones. NUMBER); <br/>}while (phoneCursor. moveToNext (); <br/>}</p> <p> // close while finish <br/> if (phoneCursor! = Null) {<br/> phoneCursor. close (); <br/>} 

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 };</p> <p> Cursor emailCursor = getContentResolver (). query (Contacts. contactMethods. CONTENT_URI, <br/> emailProjection, // select <br/> Contacts. contactMethods. PERSON_ID + "=" + id, // where <br/> null, <br/> Contacts. contactMethods. DEFAULT_SORT_ORDER); // order </p> <p> if (ema IlCursor. moveToFirst () {<br/> do {<br/> int kind = emailCursor. getInt (emailCursor. getColumnIndex (Contacts. contactMethods. KIND); <br/> if (Contacts. KIND_EMAIL = kind) {<br/> email = emailCursor. getString (emailCursor. getColumnIndex (Contacts. contactMethods. DATA); <br/>}< br/>}while (emailCursor. moveToNext (); <br/>}</p> <p> // close while finish <br/> if (emailCursor! = Null) {<br/> emailCursor. close (); <br/>} 

 

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.