android根據電話號碼查詢連絡人名稱,匯出通訊錄所有連絡人的方法

來源:互聯網
上載者:User
android根據電話號碼查詢連絡人名稱,匯出通訊錄所有連絡人的方法 1 /*
 2      * 根據電話號碼取得連絡人姓名
 3      */
 4     public static String getContactNameByPhoneNumber(Context context, String address) {
 5         String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,
 6                 ContactsContract.CommonDataKinds.Phone.NUMBER };
 7 
 8         // 將自己添加到 msPeers 中
 9         Cursor 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             // 取得連絡人名字
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      * 擷取所有連絡人內容
35      * @param context
36      * @param address
37      * @return
38      */
39     public static String getContacts(Context context) {
40         StringBuilder sb = new StringBuilder();
41         
42         ContentResolver cr = context.getContentResolver();
43         Cursor 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                 //第一條不用換行
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                     // 添加Phone的資訊
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     } 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.