访问手机通讯录

权限:

  1. <!-- 读取联系人权限 -->   
  2. <uses-permission android:name="android.permission.READ_CONTACTS"/> 
  3. <!-- 拨打电话权限 --> 
  4. <uses-permission android:name="android.permission.CALL_PHONE"/>   
字段设置:

  1.   private static final String[] PHONES_PROJECTION = new String[] {  
  2.         Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID,Phone.CONTACT_ID };  
  3.      
  4.     /**联系人显示名称**/  
  5.     private static final int PHONES_DISPLAY_NAME_INDEX = 0;  
  6.       
  7.     /**电话号码**/  
  8.     private static final int PHONES_NUMBER_INDEX = 1;  
  9.       
  10.     /**头像ID**/  
  11.     private static final int PHONES_PHOTO_ID_INDEX = 2;  
  12.      
  13.     /**联系人的ID**/  
  14.     private static final int PHONES_CONTACT_ID_INDEX = 3;  
  15.       
  16.  
  17.     /**联系人名称**/  
  18.     private ArrayList<String> mContactsName = new ArrayList<String>();  
  19.       
  20.     /**联系人头像**/  
  21.     private ArrayList<String> mContactsNumber = new ArrayList<String>();  
  22.  
  23.     /**联系人头像**/  
  24.     private ArrayList<Bitmap> mContactsPhonto = new ArrayList<Bitmap>();
正文: 用contentResolver访问 Cursor返回结果
  1.  mListView = this.getListView();  
  2.     /**得到手机通讯录联系人信息**/  
  3.     getPhoneContacts();  
  4.  
  5.     myAdapter = new MyListAdapter(this);  
  6.     setListAdapter(myAdapter);  
  7.  
  8.  
  9.     mListView.setOnItemClickListener(new OnItemClickListener() {  
  10.  
  11.         @Override  
  12.         public void onItemClick(AdapterView<?> adapterView, View view,  
  13.             int position, long id) {  
  14.         //调用系统方法拨打电话  
  15.         Intent dialIntent = new Intent(Intent.ACTION_CALL, Uri  
  16.             .parse("tel:" + mContactsNumber.get(position)));  
  17.         startActivity(dialIntent);  
  18.         }  
  19.     });  
  20.  
  21.     super.onCreate(savedInstanceState);  
  22.     }  
  23.  
  24.     /**得到手机通讯录联系人信息**/  
  25.     private void getPhoneContacts() {  
  26.     ContentResolver resolver = mContext.getContentResolver();  
  27.  
  28.     // 获取手机联系人  
  29.     Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,PHONES_PROJECTION, null, null, null);  
  30.  
  31.  
  32.     if (phoneCursor != null) {  
  33.         while (phoneCursor.moveToNext()) {  
  34.  
  35.         //得到手机号码  
  36.         String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);  
  37.         //当手机号码为空的或者为空字段 跳过当前循环  
  38.         if (TextUtils.isEmpty(phoneNumber))  
  39.             continue;  
  40.           
  41.         //得到联系人名称  
  42.         String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX);  
  43.           
  44.         //得到联系人ID  
  45.         Long contactid = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX);  
  46.  
  47.         //得到联系人头像ID  
  48.         Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);  
  49.           
  50.         //得到联系人头像Bitamp  
  51.         Bitmap contactPhoto = null;  
  52.  
  53.         //photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的  
  54.         if(photoid > 0 ) {  
  55.             Uri uri =ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactid);  
  56.             InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);  
  57.             contactPhoto = BitmapFactory.decodeStream(input);  
  58.         }else {  
  59.             contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.contact_photo);  
  60.         }  
  61.           
  62.         mContactsName.add(contactName);  
  63.         mContactsNumber.add(phoneNumber);  
  64.         mContactsPhonto.add(contactPhoto);  
  65.         }  
  66.  
  67.         phoneCursor.close();  
  68.     }  
  69.     }  
  70.       
  71.     /**得到手机SIM卡联系人人信息**/  
  72.     private void getSIMContacts() {  
  73.     ContentResolver resolver = mContext.getContentResolver();  
  74.     // 获取Sims卡联系人  
  75.     Uri uri = Uri.parse("content://icc/adn");  
  76.     Cursor phoneCursor = resolver.query(uri, PHONES_PROJECTION, null, null,  
  77.         null);  
  78.  
  79.     if (phoneCursor != null) {  
  80.         while (phoneCursor.moveToNext()) {  
  81.  
  82.         // 得到手机号码  
  83.         String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);  
  84.         // 当手机号码为空的或者为空字段 跳过当前循环  
  85.         if (TextUtils.isEmpty(phoneNumber))  
  86.             continue;  
  87.         // 得到联系人名称  
  88.         String contactName = phoneCursor 
  89.             .getString(PHONES_DISPLAY_NAME_INDEX);  
  90.  
  91.         //Sim卡中没有联系人头像  
  92.           
  93.         mContactsName.add(contactName);  
  94.         mContactsNumber.add(phoneNumber);  
  95.         }  
  96.  
  97.         phoneCursor.close();  
  98.     }  
  99.     }  
  100.       
  101.     class MyListAdapter extends BaseAdapter {  
  102.     public MyListAdapter(Context context) {  
  103.         mContext = context;  
  104.     }  
  105.  
  106.     public int getCount() {  
  107.         //设置绘制数量  
  108.         return mContactsName.size();  
  109.     }  
  110.  
  111.     @Override  
  112.     public boolean areAllItemsEnabled() {  
  113.         return false;  
  114.     }  
  115.  
  116.     public Object getItem(int position) {  
  117.         return position;  
  118.     }  
  119.  
  120.     public long getItemId(int position) {  
  121.         return position;  
  122.     }  
  123.  
  124.     public View getView(int position, View convertView, ViewGroup parent) {  
  125.         ImageView iamge = null;  
  126.         TextView title = null;  
  127.         TextView text = null;  
  128.         if (convertView == null) {  
  129.         convertView = LayoutInflater.from(mContext).inflate(  
  130.             R.layout.colorlist, null);  
  131.         iamge = (ImageView) convertView.findViewById(R.id.color_image);  
  132.         title = (TextView) convertView.findViewById(R.id.color_title);  
  133.         text = (TextView) convertView.findViewById(R.id.color_text);  
  134.         }  
  135.         //绘制联系人名称  
  136.         title.setText(mContactsName.get(position));  
  137.         //绘制联系人号码  
  138.         text.setText(mContactsNumber.get(position));  
  139.         //绘制联系人头像  
  140.         iamge.setImageBitmap(mContactsPhonto.get(position));  
  141.         return convertView;  
  142.     }  
  143.  
  144.    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值