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()) {
                      
          // 獲取聯(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 閱讀(781) 評論(0)  編輯  收藏 所屬分類: Android
          主站蜘蛛池模板: 安义县| 汶上县| 乌拉特前旗| 江门市| 呈贡县| 广平县| 聊城市| 淮阳县| 天长市| 武安市| 雷山县| 夹江县| 涟源市| 灌云县| 盐山县| 乐至县| 河间市| 临潭县| 延边| 巴里| 石景山区| 锡林浩特市| 怀宁县| 新龙县| 和田市| 遂川县| 平塘县| 乌审旗| 神农架林区| 尤溪县| 大英县| 纳雍县| 舒兰市| 洪洞县| 巢湖市| 尤溪县| 托克托县| 金坛市| 石渠县| 姜堰市| 交城县|