Read contacts and Add contacts

Source: Internet
Author: User

Bean class Contact:

1 Importjava.util.ArrayList;2 Importjava.util.List;3 4  Public classContact {5     PrivateString name;6     PrivateString Email;7     PrivateString address;8     Privatelist<string> phone =NewArraylist<string>();9 Ten      PublicContact () { One         Super(); A         //TODO auto-generated Constructor stub -     } -  the      PublicContact (string name, string email, address string, list<string>phone) { -         Super(); -          This. Name =name; -          This. email =email; +          This. Address =address; -          This. Phone =phone; +     } A  at      PublicString GetName () { -         returnname; -     } -  -      Public voidsetName (String name) { -          This. Name =name; in     } -  to      PublicString Getemail () { +         returnemail; -     } the  *      Public voidsetemail (String email) { $          This. email =email;Panax Notoginseng     } -  the      PublicString getaddress () { +         returnaddress; A     } the  +      Public voidsetaddress (String address) { -          This. Address =address; $     } $  -      PublicList<string>Getphone () { -         returnphone; the     } - Wuyi      Public voidSetphone (list<string>phone) { the          This. Phone =phone; -     } Wu}
Contact

To read a contact:

1 Importjava.util.ArrayList;2 Importjava.util.List;3 4 Importandroid.app.Activity;5 ImportAndroid.content.ContentResolver;6 ImportAndroid.database.Cursor;7 ImportAndroid.net.Uri;8 ImportAndroid.os.Bundle;9 ImportAndroid.util.Log;Ten  Public classMainactivityextendsActivity { One     Private Static FinalString TAG = "Mainactivity"; A @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); the Setcontentview (r.layout.activity_main); - readcontact (); -     } -  +      PublicList<contact>readcontact () { -List<contact>list; +Contentresolver resolver =getcontentresolver (); AUri Raw_contacturi = Uri.parse ("Content://com.android.contacts/raw_contacts"); atcursor cursor = resolver.query (Raw_contacturi,NewString[] {"contact_id" },  -                 NULL,NULL,NULL); -         if(Cursor! =NULL&& cursor.getcount () > 0) { -list=NewArraylist<contact>(); -              while(Cursor.movetonext ()) { -Integer contact_id = cursor.getint (0); inUri Datauri = Uri.parse ("Content://com.android.contacts/data"); -String selection = "raw_contact_id=?"; tostring[] Selectionargs =Newstring[] {string.valueof (contact_id)}; +Cursor cursor_data = Resolver.query (Datauri,NewString[] {"MimeType",  -"Data1"}, Selection, theSelectionargs,NULL); *                 if(Cursor_data! =NULL&& cursor_data.getcount () > 0) { $Contact Contact =NewContact ();Panax Notoginseng                      while(Cursor_data.movetonext ()) { -String mimetype = cursor_data.getstring (0); theString data1 = cursor_data.getstring (1); +                         if("Vnd.android.cursor.item/name". Equals (MimeType)) { A Contact.setname (data1); the}Else if("Vnd.android.cursor.item/postal-address_v2". Equals (MimeType)) { + contact.setaddress (data1); -}Else if("Vnd.android.cursor.item/email_v2". Equals (MimeType)) { $ Contact.setemail (data1); $}Else if("Vnd.android.cursor.item/phone_v2". Equals (MimeType)) { - Contact.getphone (). Add (data1); -                         } the List.add (contact); -LOG.I (TAG, "mimetype=" + mimetype + "\tdata1=" +data1);Wuyi                     } the cursor_data.close (); -                 } Wu             } - cursor.close (); AboutLOG.I (TAG, "----------"); $             returnlist; -         } -         return NULL; -     } A  +}
readcontact

To add a contact:

 Public voidwritecontact () {//1. Access to the content provider's tool classContentresolver resolver =Getcontentresolver (); //2. Get to the contact_id of the last contact in the contact database, and then add 1 is the contact_id of the exercises you are adding nowUri Raw_contact_uri = Uri.parse ("Content://com.android.contacts/raw_contacts"); Cursor Cursor= Resolver.query (Raw_contact_uri,Newstring[]{"contact_id"},NULL,                 NULL, "contact_id desc LIMIT 1"); Integer max_contact_id= 0; if(NULL! = Cursor &&Cursor.movetofirst ()) {max_contact_id= Cursor.getint (0); } max_contact_id= max_contact_id + 1; //3. Add contact_id to the Raw_contact tableContentvalues values =Newcontentvalues (); Values.put ("CONTACT_ID", max_contact_id); Raw_contact_uri=Resolver.insert (Raw_contact_uri, values); LOG.I (TAG,"Raw_contact_uri=" +Raw_contact_uri); //4. Add data (name, Email,address,phone and other information)//Note: One message is the same record, so multiple insert operations are performed when adding multiple information for a contactUri Data_uri = Uri.parse ("Content://com.android.contacts/data"); Values.clear (); //be sure to empty before reusing or it will affect the following insert OperationValues.put ("raw_contact_id", max_contact_id); Values.put ("MimeType", "Vnd.android.cursor.item/name"); Values.put ("Data1", "1505");                Resolver.insert (Data_uri, values); Values.clear (); //be sure to empty before reusing or it will affect the following insert OperationValues.put ("raw_contact_id", max_contact_id); Values.put ("MimeType", "VND.ANDROID.CURSOR.ITEM/EMAIL_V2"); Values.put ("Data1", "[Email protected],com");                Resolver.insert (Data_uri, values); Values.clear (); //be sure to empty before reusing or it will affect the following insert OperationValues.put ("raw_contact_id", max_contact_id); Values.put ("MimeType", "VND.ANDROID.CURSOR.ITEM/PHONE_V2"); Values.put ("Data1", "16824524");                Resolver.insert (Data_uri, values); Values.clear (); //be sure to empty before reusing or it will affect the following insert OperationValues.put ("raw_contact_id", max_contact_id); Values.put ("MimeType", "VND.ANDROID.CURSOR.ITEM/POSTAL-ADDRESS_V2"); Values.put ("Data1", "Xi ' an Road");    Resolver.insert (Data_uri, values); }
writecontact

Read contacts and Add contacts

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.