Black Horse Programmer-----Android Common URI

Source: Internet
Author: User

Content://com.android.contacts/contacts

The Contacts table in the corresponding database table is used to get the contact ID keyword "_id"


content://com.android.contacts/contacts/"+ contactid+"/data

The data table uses ContactID to find the various properties of the contact (Data1 property value data2 The type of attribute (e.g. home phone) keyword "mimetype", "Data1", "Data2"



"content://com.android.contacts/data/phones/filter/" + number

Find Contact name keyword "Display_name" by phone number


Content://com.android.contacts/raw_contacts

Add a Contact

Example:

Add Name
Values.put ("raw_contact_id", ContactID);
Values.put ("MimeType", "vnd.android.cursor.item/name");
Values.put ("Data2", "Zhang Small");
Resolver.insert (URI, values);
Add phone
Values.clear ();
Values.put ("raw_contact_id", ContactID);
Values.put ("MimeType", "vnd.android.cursor.item/phone_v2");
Values.put ("Data2", "2");
Values.put ("Data1", "13671323507");
Resolver.insert (URI, values);
Add Email
Values.clear ();
Values.put ("raw_contact_id", ContactID);
Values.put ("MimeType", "vnd.android.cursor.item/email_v2");
Values.put ("Data2", "2");
Values.put ("Data1", "[email protected]");
Resolver.insert (URI, values);



Package Cn.itcast.test;import Java.util.arraylist;import Android.content.contentprovideroperation;import Android.content.contentresolver;import Android.content.contenturis;import Android.content.contentvalues;import Android.database.cursor;import Android.net.uri;import Android.test.androidtestcase;import Android.util.Log;public Class Contactstest extends Androidtestcase {private static final String TAG = "contactstest";//Get all contacts public void Testcont Acts () throws Exception{uri Uri = Uri.parse (<strong> "content://com.android.contacts/contacts</strong>") ; Contentresolver resolver = GetContext (). Getcontentresolver (); cursor cursor = resolver.query (URI, new string[]{"_id"}, NULL, NULL, NULL), while (Cursor<strong>.movetonext () < /strong>) {int contactid = cursor.getint (0); StringBuilder sb = new StringBuilder ("contactid="); Sb.append (contactid); uri = Uri.parse ("<strong>content:// com.android.contacts/contacts/"+ contactid+"/data "</strong>) <span style=" COLOR: #ff0000; " >//can be directly obtained mimetype</span>cursor Datacursor = Resolver.query (URI, new string[]{<, because contacts is used in the correlation query) Strong> "MimeType", "Data1", "Data2" &LT;/STRONG&GT;}, NULL, NULL, NULL); while (Datacursor.<strong>movetonext () </strong>) {String data = datacursor.getstring (Datacursor.<strong>getcolumnindex</strong> (" Data1 ")); String type = datacursor.getstring (Datacursor.getcolumnindex ("MimeType")),//mimetype the value of the object in the table if (" Vnd.android.cursor.item/name ". Equals (Type)) {//Name Sb.append (", name= "+ data);} else if ("Vnd.android.cursor.item/email_v2". Equals (Type)) {//emailsb.append (", email=" + data);} else if ("Vnd.android.cursor.item/phone_v2". Equals (Type)) {//phonesb.append (", phone=" + data);}} LOG.I (TAG, sb.tostring ());}} <span style= "color: #ff0000;" >//gets the name of the contact according to the number </span>public void Testcontactnamebynumber () throws exception{string numbers = "18601025011"; Uri uri = uri.parse (<strong> "content://com.android.contacts/data/phones/filter/" + number</strong>); ContentresolVer resolver = GetContext (). Getcontentresolver (); cursor cursor = resolver.query (URI, new string[]{<strong> "Display_name" &LT;/STRONG&GT;}, NULL, NULL, NULL); if ( Cursor.movetofirst ()) {String name = cursor.getstring (0); LOG.I (TAG, name);} Cursor.close ();} <span style= "color: #ff0000;" >//Add contact </span>public void Testaddcontact () throws Exception{uri Uri = Uri.parse ("content:// Com.android.contacts/raw_contacts "); Contentresolver resolver = GetContext (). Getcontentresolver (); Contentvalues values = new Contentvalues ();//<strong><span style= "color: #ff0000;" > Returns a Contactid</span></strong>long ContactID = Contenturis.parseid by performing an insert () NULL operation (Resolver.insert (URI, values)); URI = Uri.parse ("Content://com.android.contacts/data");//Add Name Values.put ("raw_contact_id", ContactID); Values.put (" MimeType "," Vnd.android.cursor.item/name "); Values.put (" Data2 "," Zhang Xiao "); Resolver.<strong>insert (URI, values); </strong>//Add phone values.clear (); Values.put ("raw_contact_id", contactID), Values.put ("MimeType", "Vnd.android.cursor.item/phone_v2"), Values.put ("Data2", "2"), Values.put ("Data1", " 13671323507 "); Resolver.insert (URI, values);//Add emailvalues.clear (); Values.put (" raw_contact_id ", ContactID); Values.put ("MimeType", "Vnd.android.cursor.item/email_v2"), Values.put ("Data2", "2"), Values.put ("Data1", "[email  protected] "); Resolver.insert (URI, values);} <span style= "color: #ff0000;" ><strong>//Complete the addition of contact data in the same transaction </strong></span>public void TestAddContact2 () throws exception{ Uri uri = uri.parse ("content://com.android.contacts/raw_contacts"); Contentresolver resolver = GetContext (). Getcontentresolver (); arraylist<contentprovideroperation> operations = new arraylist<contentprovideroperation> ();
<span style= "White-space:pre" ></span>< Strong><span style= "color: #ff6666;" >//No. 0 operation toward </span><span style= "Color:rgb (255, 102, 102); Font-family:arial, Helvetica, Sans-serif; Insert a data in the >raw_contacts table </span></strong><span style= "Color:rgb (255, 102, 102); Font-family:arial, Helvetica, Sans-serif; ><strong></strong></span> 
<span style= "font-family:arial, Helvetica, Sans-serif;" ><span style= "FONT-SIZE:14PX; White-space:pre; " ></span> <span style= "FONT-SIZE:12PX;" >contentprovideroperation OP1 = Contentprovideroperation.newinsert (URI) </span></span><span style = "COLOR: #ff6666; font-size:14px;" ></span><span style= "FONT-SIZE:14PX;" >.withvalue ("account_name", null). Build (); Operations.add (OP1); uri = Uri.parse ("content://com.android.contacts/ Data "); Contentprovideroperation op2 = Contentprovideroperation.newinsert (URI) </span> 
<span style= "font-size:14px; white-space:pre;" ></span><strong><span style= "color: #ff0000;" >!!!! <span style= "FONT-SIZE:14PX;" >//according to the return ID of the No. 0 operation as </span><span style= "font-family:arial, Helvetica, Sans-serif; font-size:14px; " The value of the >raw_contact_id field </span><span style= "font-family:arial, Helvetica, Sans-serif;" ><span style= "FONT-SIZE:14PX;" ></span></span></span></strong> 
<span style= "font-family:arial, Helvetica, Sans-serif;" ><span style= "FONT-SIZE:12PX;" ><span style= "White-space:pre" ></span><strong><span style= "color: #ff0000;" >.withvaluebackreference ("raw_contact_id", 0) </span></strong></span></span><span Style= "FONT-SIZE:14PX;" ></span><span style= "FONT-SIZE:14PX;" >.withvalue ("MimeType", "Vnd.android.cursor.item/name"). Withvalue ("Data2", "Bruce Lee"). Build (); Operations.add (OP2) ; Contentprovideroperation op3 = Contentprovideroperation.newinsert (URI). Withvaluebackreference ("raw_contact_id", 0) . Withvalue ("MimeType", "Vnd.android.cursor.item/phone_v2"). Withvalue ("Data1", "13560650505"). Withvalue ("Data2", " 2 "). Build (); Operations.add (OP3); Contentprovideroperation OP4 = Contentprovideroperation.newinsert (URI). Withvaluebackreference ("raw_contact_id", 0) . Withvalue ("MimeType", "Vnd.android.cursor.item/email_v2"). Withvalue ("Data1", "[email protected]"). Withvalue ("Data2", "2"). Build (); opErations.add (OP4); Resolver.applybatch ("com.android.contacts", Operations);}} </span><strong style= "FONT-SIZE:14PX;" ></strong>

withvaluebackreference(String key, int previousresult) method

The first parameter key corresponds to the column name in the database, and the second parameterPreviousresultRepresents: In the batch operation of the back-up databaseSection PreviousresultThe return value of a database operation. In general, the return value of the Previousresult database operation in the bulk database operation as the value of the record with the column name key







Black Horse Programmer-----Android Common URI

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.