package yinger.firewall.util;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;

/**
 * ReadContactsUtil
 * 
 * 
@author Ying_er
 * @Email melody.crazycoding@gmail.com
 * @time 2011/03/30 11:57:23
 * 
@version 1.00
 
*/
public final class MyReadContactsUtil {
    
/**
     * 
@return contacts list
     
*/
    
public static ArrayList getContacts(Context context) {
        ArrayList
<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        String number 
= "";
        ContentResolver cr 
= context.getContentResolver();// 得到一个ContentResolver对象
        
// 得到一个游标查询对象
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                
nullnullnull);
        
while (cursor.moveToNext()) {
            
// 获取联系人的名字的索引
            int nameFieldColumnIndex = cursor
                    .getColumnIndex(PhoneLookup.DISPLAY_NAME);
            
// 获取联系人的名字
            String contact = cursor.getString(nameFieldColumnIndex);
            Cursor target 
= context.getContentResolver().query(
                    ContactsContract.Contacts.CONTENT_URI, 
nullnullnull,
                    
null);
            target.moveToFirst();
            
// 得到联系人的ID
            String contentId = cursor.getString(target
                    .getColumnIndex(ContactsContract.Contacts._ID));
            String isPnone 
= cursor
                    .getString(cursor
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
            
if (Integer.parseInt(isPnone) > 0) {
                Cursor phone 
= context.getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        
null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
+ "="
                                
+ contentId, nullnull);
                
while (phone.moveToNext()) {
                    
// 得到联系人电话
                    number = phone
                            .getString(phone
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    HashMap
<String, String> map = new HashMap<String, String>();
                    map.put(
"number", number);
                    map.put(
"name", contact);
                    number 
= "";
                    list.add(map);
                }
                phone.close();
            }
        }
        cursor.close();
        
return list;
    }
}
posted on 2011-05-26 10:05 Ying-er 阅读(774) 评论(0)  编辑  收藏 所属分类: Android

只有注册用户登录后才能发表评论。


网站导航: