Android queries the contact name by phone number and exports all contacts in the address book

Source: Internet
Author: User
Android queries the contact name based on the phone number and exports all contacts in the address book. method 1 /*
2 * obtain the contact name based on the phone number
3 */
4 public static string getcontactnamebyphonenumber (context, string address ){
5 string [] projection = {contactscontract. phonelookup. display_name,
6 contactscontract. commondatakinds. Phone. Number };
7
8 // Add yourself to mspeers
9 cursor = context. getcontentresolver (). Query (
10 contactscontract. commondatakinds. Phone. content_uri,
11 projection, // which columns to return.
12 contactscontract. commondatakinds. Phone. Number + "= '"
13 + address + "'", // where clause.
14 null, // where clause value substitution
15 null); // sort order.
16
17 if (cursor = NULL ){
18 log. D (TAG, "getpeople null ");
19 return NULL;
20}
21 For (INT I = 0; I <cursor. getcount (); I ++ ){
22 cursor. movetoposition (I );
23
24 // get the contact name
25 int namefieldcolumnindex = cursor
26. getcolumnindex (contactscontract. phonelookup. display_name );
27 string name = cursor. getstring (namefieldcolumnindex );
28 return name;
29}
30 return NULL;
31}
32
33 /**
34 * retrieve all contacts
35 * @ Param Context
36 * @ Param address
37 * @ return
38 */
39 public static string getcontacts (context ){
40 stringbuilder sb = new stringbuilder ();
41
42 contentresolver Cr = context. getcontentresolver ();
43 cursor = Cr. Query (contactscontract. Contacts. content_uri, null,
44 null, null, null );
45
46 If (cursor. movetofirst ()){
47 do {
48 string contactid = cursor. getstring (cursor
49. getcolumnindex (contactscontract. Contacts. _ id ));
50 string name = cursor
51. getstring (cursor
52. getcolumnindex (contactscontract. Contacts. display_name ));
53 // line feed is not required for the first line
54 if (sb. Length () = 0 ){
55 sb. append (name );
56} else {
57 sb. append ("\ n" + name );
58}
59
60 cursor phones = Cr. Query (
61 contactscontract. commondatakinds. Phone. content_uri,
62 null, contactscontract. commondatakinds. Phone. contact_id
63 + "=" + contactid, null, null );
64 While (phones. movetonext ()){
65 string phonenumber = phones
66. getstring (Phones
67. getcolumnindex (contactscontract. commondatakinds. Phone. Number ));
68 // Add phone information
69 sb. append ("\ t"). append (phonenumber );
70
71}
72 phones. Close ();
73
74} while (cursor. movetonext ());
75}
76 cursor. Close ();
77 return sb. tostring ();

78}

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.