搜尋此網誌

2011年7月12日

android 通訊錄存取(取得連絡人)

在網路上參考的一些範例來取得連絡人資料

Reference

ContentResolver (This class provides applications access to the content model)
ContactsContract.Contacts (Constants for the contacts table, which contains a record per aggregate of raw contacts representing the same person.)

宣告ContectResolver並取得通訊錄內資料
ContentResolver cr = getContentResolver();
Cursor cur = cr.query (ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
public final Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

Since: API Level 1
Query the given URI, returning a Cursor over the result set.
For best performance, the caller should follow these guidelines:
Provide an explicit projection, to prevent reading data from storage that aren't going to be used.
Use question mark parameter markers such as 'phone=?' instead of explicit values in the selection parameter, so that queries that differ only by those values will be recognized as the same for caching purposes.

Parameters
uri The URI, using the content:// scheme, for the content to retrieve.
projection A list of which columns to return. Passing null will return all columns, which is inefficient.
selection A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given URI.
selectionArgs You may include ?s in selection, which will be replaced by the values from selectionArgs, in the order that they appear in the selection. The values will be bound as Strings.
sortOrder How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.

Returns
A Cursor object, which is positioned before the first entry, or null


取得 contact ID
取得系統中連絡人 RawID,利用該ID串其他如電話,地址,公司等欄位資料
while (cur.moveToNext()) {
    String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
}

取得連絡人名字(Display Name)
cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))

取得連絡人電話資料
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
              new String[]{id}, null);
while (pCur.moveToNext()) {
int code = Integer.valueOf(pCur.getString(
              pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)));

switch(code){
    case 1://Home
        pCur.getString(pCur.getColumnIndex(
                 ContactsContract.CommonDataKinds.Phone.NUMBER));
 break;
    case 2://Mobile
        pCur.getString(pCur.getColumnIndex(
                 ContactsContract.CommonDataKinds.Phone.NUMBER)));
        break;
    case 3://Work
        pCur.getString(pCur.getColumnIndex(
                 ContactsContract.CommonDataKinds.Phone.NUMBER)));
 break;
    }
} 
pCur.close();

取得Email 資訊
Cursor emailCur = cr.query( ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                 null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
                 new String[]{id}, null); 
while (emailCur.moveToNext()) { 
int code = Integer.valueOf(emailCur.getString(emailCur.getColumnIndex(
                 ContactsContract.CommonDataKinds.Email.TYPE)));

switch(code){
    case 1://Work
        emailCur.getString(emailCur.getColumnIndex(
                 ContactsContract.CommonDataKinds.Email.DATA));
 break;
    }           
} 
emailCur.close();

取得地址資訊
String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " +   
                   ContactsContract.Data.MIMETYPE + " = ?"; 

String[] addrWhereParams = new String[]{id,
            ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE}; 

Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI, 
            null, addrWhere, addrWhereParams, null); 

while(addrCur.moveToNext()) {
    String street = addrCur.getString(addrCur.getColumnIndex(
                    ContactsContract.CommonDataKinds.StructuredPostal.STREET));
    String city = addrCur.getString(addrCur.getColumnIndex(
                    ContactsContract.CommonDataKinds.StructuredPostal.CITY));
    String state = addrCur.getString(addrCur.getColumnIndex(
                    ContactsContract.CommonDataKinds.StructuredPostal.REGION));
    String postalCode = addrCur.getString(addrCur.getColumnIndex(
                    ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
    String country = addrCur.getString(addrCur.getColumnIndex(
                    ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
    int code = Integer.valueOf(addrCur.getString(addrCur.getColumnIndex(
                    ContactsContract.CommonDataKinds.StructuredPostal.TYPE)));
    switch(code){
        case 1:
            //住家地址
            street+city+state+country+postalCode;
     break;
        case 2:
            //辦公室地址
     street+city+state+country+postalCode;
            break;
 }       
}
addrCur.close();

取得公司資訊
String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + 
                  ContactsContract.Data.MIMETYPE + " = ?"; 

String[] orgWhereParams = new String[]{id,  
                ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE}; 

Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI, 
                null, orgWhere, orgWhereParams, null);

if (orgCur.moveToFirst()) { 
      orgCur.getString(orgCur.getColumnIndex(
                ContactsContract.CommonDataKinds.Organization.DATA)));       
} 
orgCur.close();

沒有留言:

張貼留言