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();// 得到一個(gè)ContentResolver對(duì)象
                  
          // 得到一個(gè)游標(biāo)查詢(xún)對(duì)象
                  Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                          
          nullnullnull);
                  
          while (cursor.moveToNext()) {
                      
          // 獲取聯(lián)系人的名字的索引
                      int nameFieldColumnIndex = cursor
                              .getColumnIndex(PhoneLookup.DISPLAY_NAME);
                      
          // 獲取聯(lián)系人的名字
                      String contact = cursor.getString(nameFieldColumnIndex);
                      Cursor target 
          = context.getContentResolver().query(
                              ContactsContract.Contacts.CONTENT_URI, 
          nullnullnull,
                              
          null);
                      target.moveToFirst();
                      
          // 得到聯(lián)系人的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()) {
                              
          // 得到聯(lián)系人電話
                              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 閱讀(785) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Android
          主站蜘蛛池模板: 师宗县| 景宁| 陕西省| 乐至县| 门头沟区| 黎城县| 广元市| 兴和县| 丘北县| 霍城县| 额尔古纳市| 白河县| 阿城市| 锦州市| 新蔡县| 普格县| 永平县| 廉江市| 冷水江市| 上思县| 安福县| 商丘市| 高邮市| 铁岭县| 山西省| 库尔勒市| 阿勒泰市| 崇阳县| 禹城市| 四子王旗| 伊宁市| 资中县| 台州市| 宁南县| 杨浦区| 内黄县| 女性| 梨树县| 内江市| 贡山| 新丰县|