"Android Basics" reads system contact information and adds a contact information to the Address Book

Source: Internet
Author: User

One, read the system contact information.

I, preparatory work

Similarly, to read the database files for a system contact, first determine the hostname and matching rules.

When we export a contact's database file to view, there are a few things to know:

1) Contact data, such as Name,number,email, are stored in the Data1 column of the data table.

2) The column raw_contact_id that distinguishes between different contacts, which corresponds to the contact_id columns in the Raw_contacts table

3) The column that distinguishes the contact data type is in the MimeType column, where 1 means emial,5 represents the phone and 7 represents the name.

II, ideas

Read the contact information, that is, read the data table name, phone, mailbox.

Does this write: SELECT * from data.

The above SQL statement looks for the entire table, and we should look up the data for each contact based on the ID of the identity contact.

So, the first thing to do is to query the contact_id in the Raw_contacts table, and then go to the data table based on the ID.

1Contentresolver resolver=getcontentresolver ();2Uri uri=uri.parse ("Content://com.android.contacts/raw_contacts");3Uri datauri=uri.parse ("Content://com.android.contacts/data");4Cursor Cursor=resolver.query (URI,NULL,NULL,NULL,NULL);5       while(Cursor.movetonext ()) {6String id=cursor.getstring (Cursor.getcolumnindex ("contact_id"));7}

Once you get the ID, you can query the data table based on the ID, so the code is well written:

1  while(Cursor.movetonext ()) {2String id=cursor.getstring (Cursor.getcolumnindex ("contact_id"));3             /**4              *Once you get the ID of a contact, you can query the data table for content and type based on the ID .6 The ID is not NULL, indicating that the contact also exists. Otherwise, if you delete the contact and then look for it, a null pointer exception will occur. 7              */8             if(id!=NULL){9Cursor Datacursor=resolver.query (Datauri,NULL, "Raw_contact_id=?",NewString[]{id},NULL);Ten                  while(Datacursor.movetonext ()) { OneString data=datacursor.getstring (Datacursor.getcolumnindex ("Data1")); AString mimetype=datacursor.getstring (Datacursor.getcolumnindex ("MimeType")); -System.out.println (data+ "" +mimetype); -                 } the             } -              -}

Why does a null pointer exception occur?

When a contact is deleted, the contact's ID in the raw_contacts table is null, but the associated data is not deleted in the data table. Therefore, if you do not determine whether the ID is empty, the program will use NULL to go to the data table to query.

So why does deleting a contact do not delete data from the database table?

To delete a contact, simply mark the contact's ID as empty, and there's a benefit: because these contacts need to be uploaded to the server, you can restore your contacts if they are lost due to various reasons such as swiping.
If you are offline, that is, when you are not connected to the server, delete the contact person. When there is a network, the server spends a lot of time knowing which contacts have been deleted.

At this point, simply set the ID to null, it is convenient to know what the contact was deleted.

Second, add contacts to your contacts.

I, thinking

The key to adding a contact's information to the data table is to get the contact_id in the Raw_contacts table of the contact person.

This ID value is the last ID plus 1.

So the code is:

1Contentresolver resolver=getcontentresolver ();2Uri uri=uri.parse ("Content://com.android.contacts/raw_contacts");3Uri datauri=uri.parse ("Content://com.android.contacts/data");4         5         /**6 * To add an ID to the Raw_contact table, you must know the ID of the last contact in the table7          */8         9         //The second parameter represents the contentTenCursor Cursor=resolver.query (URI,Newstring[]{"_id"},NULL,NULL,NULL); One         //cursor moves to the last row A cursor.movetolast (); -         //get the ID of the last contact -         intOldid=cursor.getint (Cursor.getcolumnindex ("_id")); the          -         intNewid=oldid+1; -Contentvalues values=Newcontentvalues (); -Values.put ("contact_id", NewID);
Resolver.insert (URI, values);

Once you add a new ID to the Raw_contacts table, you can use this ID to add the contact's data to the database table.

1 //insert data into the database table using the ID added by the contact2         //Add a number3Contentvalues phonevalues=Newcontentvalues ();4Phonevalues.put ("Data1", "88888");5Phonevalues.put ("MimeType", "VND.ANDROID.CURSOR.ITEM/PHONE_V2");6Phonevalues.put ("raw_contact_id", NewID);7 Resolver.insert (Datauri, phonevalues);8         9         //Add NameTenContentvalues namevalues=Newcontentvalues (); OneNamevalues.put ("Data1", "Oooo"); ANamevalues.put ("MimeType", "Vnd.android.cursor.item/name"); -Namevalues.put ("raw_contact_id", NewID); - Resolver.insert (Datauri, namevalues); the          -         //Add a mailbox -Contentvalues emailvalues=Newcontentvalues (); -Emailvalues.put ("Data1", "[email protected]"); +Emailvalues.put ("MimeType", "VND.ANDROID.CURSOR.ITEM/EMAIL_V2"); -Emailvalues.put ("raw_contact_id", NewID); + Resolver.insert (Datauri, emailvalues); A          atToast.maketext ( This, "Add contact succeeded", 0). Show ();

"Android Basics" reads system contact information and adds a contact information to the Address Book

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.