woong's

Android 내부 연락처 정보 가져오기 본문

Develop/Android

Android 내부 연락처 정보 가져오기

dlsdnd345 2016. 2. 14. 12:57

Android 내부 연락처 정보 가져오기 



 

Colored By Color Scripter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.handstudio.android.utils;
 
import java.util.ArrayList;
 
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
 
import com.handstudio.android.vo.Contact;
 
public class ContactUtil {
 
    private Context context;
    
    public ContactUtil(Context context){
        this.context = context;
    }
    
    /**
     * 내장 연락처 받기
     * ID , 이름 , 번호 추출
     * @return
     */
    public ArrayList<Contact> getContactList() {
 
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
 
        String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID, // 연락처 ID -> 사진 정보 가져오는데 사용
                ContactsContract.CommonDataKinds.Phone.NUMBER,        // 연락처
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME }; // 연락처 이름.
 
        String[] selectionArgs = null;
 
        String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                + " COLLATE LOCALIZED ASC";
 
        Cursor contactCursor = context.getContentResolver().query(uri, projection, null,selectionArgs, sortOrder);
 
        ArrayList<Contact> contactlist = new ArrayList<Contact>();
 
        if (contactCursor.moveToFirst()) {
            do {
                String phonenumber = contactCursor.getString(1).replaceAll("-","");
                if (phonenumber.length() == 10) {
                    phonenumber = phonenumber.substring(0, 3) + "-"
                            + phonenumber.substring(3, 6) + "-"
                            + phonenumber.substring(6);
                } else if (phonenumber.length() > 8) {
                    phonenumber = phonenumber.substring(0, 3) + "-"
                            + phonenumber.substring(3, 7) + "-"
                            + phonenumber.substring(7);
                }
 
                Contact contact = new Contact();
                contact.setId(contactCursor.getLong(0));
                contact.setPhoneNumber(phonenumber);
                contact.setName(contactCursor.getString(2));
                contactlist.add(contact);
                
            } while (contactCursor.moveToNext());
        }
 
        return contactlist;
 
    }
    
}
 


Comments