Android uses contentresolver to perform simple operations on the data in the Communication record

Source: Internet
Author: User
Tags throwable

1. Add read/write permissions

XHTML code
  1. <Uses-Permission
    Android: Name = "android. Permission. read_contacts"
    />
  2. <Uses-Permission
    Android: Name = "android. Permission. write_contacts"
    />

<Uses-Permission Android: Name = "android. Permission. read_contacts"/> <uses-Permission Android: Name = "android. Permission. write_contacts"/>

Contact information URI:

Content: // com. Android. Contacts/contacts

Contact number URI:

Content: // com. Android. Contacts/data/phones

Contact email URI:

Content: // com. Android. Contacts/data/emails


(Recommended) You can also obtain the contact information URI: URI uri = contactscontract. Contacts. content_uri;



2. query and add Contacts (Unit Test Cases)


Java code
  1. Public
    Class contacttest
    Extends androidtestcase
  2. {
  3. Private
    Static
    Final string tag =
    "Contacttest ";
  4. /**
  5. * Obtain the simple information of all contacts in the address book.
  6. * @ Throws throwable
  7. */
  8. Public
    Void testgetallcontact ()
    Throws throwable
  9. {
  10. // Obtain the URI of the contact information
  11. Uri uri = contactscontract. Contacts. content_uri;
  12. // Obtain contentresolver
  13. Contentresolver = This. getcontext (). getcontentresolver ();
  14. // Query data and return cursor
  15. Cursor cursor = contentresolver. Query (Uri, null,
    Null,
    Null,
    Null );
  16. While (cursor. movetonext ())
  17. {
  18. Stringbuilder sb = new stringbuilder ();
  19. // Obtain the contact ID
  20. String contactid = cursor. getstring (cursor. getcolumnindex (contactscontract. Contacts. _ id ));
  21. // Obtain the contact name
  22. String name = cursor. getstring (cursor. getcolumnindex (contactscontract. Contacts. display_name ));
  23. // Construct the contact information
  24. SB. append ("contactid ="). append (contactid). append (", name ="). append (name );
  25. // Query telephone data operations
  26. Cursor phones = contentresolver. Query (contactscontract. commondatakinds. Phone. content_uri,
  27. Null,
  28. Contactscontract. commondatakinds. Phone. contact_id + "=" + contactid,
  29. Null,
    Null );
  30. While (phones. movetonext ())
  31. {
  32. String phonenumber = phones. getstring (phones. getcolumnindex (
  33. Contactscontract. commondatakinds. Phone. Number ));
  34. // Add phone information
  35. SB. append (", phone ="). append (phonenumber );
  36. }
  37. Phones. Close ();
  38. // Query email-type data operations
  39. Cursor emails = contentresolver. Query (contactscontract. commondatakinds. Email. content_uri,
  40. Null,
  41. Contactscontract. commondatakinds. Email. contact_id + "=" + contactid,
  42. Null,
    Null );
  43. While (emails. movetonext ())
  44. {
  45. String emailaddress = emails. getstring (emails. getcolumnindex (
  46. Contactscontract. commondatakinds. Email. Data ));
  47. // Add email information
  48. SB. append (", email ="). append (emailaddress );
  49. }
  50. Emails. Close ();
  51. Log. I (TAG, SB. tostring ());
  52. }
  53. Cursor. Close ();
  54. }
  55. /** Method 1 for adding a contact:
  56. * First, execute a null value insert to rawcontacts. content_uri to obtain rawcontactid returned by the system.
  57. * In this case, the data table is inserted later. Only when null values are inserted can the inserted contacts be visible in the address book.
  58. */
  59. Public
    Void testinsert ()
  60. {
  61. Contentvalues values = new contentvalues ();
  62. // First, execute a null value insert to rawcontacts. content_uri to obtain rawcontactid returned by the system.
  63. Uri rawcontacturi = This. getcontext (). getcontentresolver (). insert (rawcontacts. content_uri, values );
  64. // Obtain the ID
  65. Long rawcontactid = contenturis. parseid (rawcontacturi );
  66. // Input name data to the data table
  67. Values. Clear ();
  68. Values. Put (data. raw_contact_id, rawcontactid); // Add an ID
  69. Values. Put (data. mimetype, structuredname. content_item_type); // Add content type (mimetype)
  70. Values. Put (structuredname. given_name, "Kaifeng from South"); // Add the name to the first name position.
  71. This. getcontext (). getcontentresolver (). insert (Android. provider. contactscontract. Data. content_uri, values );
  72. // Input telephone data to the data table
  73. Values. Clear ();
  74. Values. Put (data. raw_contact_id, rawcontactid );
  75. Values. Put (data. mimetype, phone. content_item_type );
  76. Values. Put (phone. Number, "13921009789 ");
  77. Values. Put (phone. type, phone. type_mobile );
  78. This. getcontext (). getcontentresolver (). insert (Android. provider. contactscontract. Data. content_uri, values );
  79. // Input email data to the data table
  80. Values. Clear ();
  81. Values. Put (data. raw_contact_id, rawcontactid );
  82. Values. Put (data. mimetype, email. content_item_type );
  83. Values. Put (email. Data, "kesenhoo@gmail.com ");
  84. Values. Put (email. type, email. type_work );
  85. This. getcontext (). getcontentresolver (). insert (Android. provider. contactscontract. Data. content_uri, values );
  86. }
  87. /** Method 2 for adding a contact:
  88. * Add Contacts in batches
  89. * @ Throws throwable
  90. */
  91. Public
    Void testsave ()
    Throws throwable
  92. {
  93. // Official Document location: reference \ Android \ provider \ contactscontract.rawcontacts.html
  94. // Create an arraylist to store batch Parameters
  95. Arraylist <contentprovideroperation> Ops = new arraylist <contentprovideroperation> ();
  96. Int rawcontactinsertindex = ops. Size ();
  97. Ops. Add (contentprovideroperation. newinsert (rawcontacts. content_uri)
  98. . Withvalue (rawcontacts. account_type, null)
  99. . Withvalue (rawcontacts. account_name, null)
  100. . Build ());
  101. // Official Document location: reference \ Android \ provider \ contactscontract.data.html
  102. // Withvaluebackreference the ID of the preceding contact
  103. Ops. Add (contentprovideroperation. newinsert (Android. provider. contactscontract. Data. content_uri)
  104. . Withvaluebackreference (data. raw_contact_id, rawcontactinsertindex)
  105. . Withvalue (data. mimetype, structuredname. content_item_type)
  106. . Withvalue (structuredname. given_name, "James ")
  107. . Build ());
  108. Ops. Add (contentprovideroperation. newinsert (Android. provider. contactscontract. Data. content_uri)
  109. . Withvaluebackreference (data. raw_contact_id, rawcontactinsertindex)
  110. . Withvalue (data. mimetype, phone. content_item_type)
  111. . Withvalue (phone. Number, "13671323809 ")
  112. . Withvalue (phone. type, phone. type_mobile)
  113. . Withvalue (phone. label, "mobile phone number ")
  114. . Build ());
  115. Ops. Add (contentprovideroperation. newinsert (Android. provider. contactscontract. Data. content_uri)
  116. . Withvaluebackreference (data. raw_contact_id, rawcontactinsertindex)
  117. . Withvalue (data. mimetype, email. content_item_type)
  118. . Withvalue (email. Data, "kesen@gmail.com ")
  119. . Withvalue (email. type, email. type_work)
  120. . Build ());
  121. Contentproviderresult [] Results = This. getcontext (). getcontentresolver ()
  122. . Applybatch (contactscontract. Authority, OPS );
  123. For (contentproviderresult result: Results)
  124. {
  125. Log. I (TAG, result. Uri. tostring ());
  126. }
  127. }
  128. }

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.