当前位置:操作系统 > 安卓/Android >>

2.0联系人增、删、改、查

本章和大家分享的是手机通讯录方面的经验,需求背景:对手机通讯录进行增、删、改、查,或者有时候App需要与手机 contacts进行交互或共享contacts。
我们都知道android系统没有完全将contacts向developer开放,开发人员只能通过android提供的有限的Api对contacts进行操作,我们只能通过ContentProvider对contacts的数据进行增、删、改、查等操作,并不能改变到其表结构。
首先,我们来看下如何通过ContentProvider查找contacts,以下我以查找其姓、名、公司、家庭电话、工作电话、移动电话、传真、邮箱、城市、省份、国家、出生年月为例:
 
 
 /**
  * 取得family_name和given_name
  */
 Cursor nameCur=cr.query(ContactsContract.Data.CONTENT_URI,
   new String[] {Data.CONTACT_ID, StructuredName.DISPLAY_NAME, StructuredName.FAMILY_NAME, StructuredName.GIVEN_NAME, StructuredName.MIDDLE_NAME},
    ContactsContract.Data.CONTACT_ID + "=?" + " AND "
          + ContactsContract.Data.MIMETYPE + "='" +  ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "'",
   new String[]{android_id}, null);
 if(nameCur.moveToFirst()){
  firstName=nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
  lastName=nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
 }
 nameCur.close();
 
 Cursor companyCur=cr.query(ContactsContract.Data.CONTENT_URI,
   new String[] {CommonDataKinds.Organization.COMPANY},
    ContactsContract.Data.CONTACT_ID + "=?" + " AND "
          + ContactsContract.Data.MIMETYPE + "='" +  ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE + "'",
   new String[]{android_id}, null);
 
 if(companyCur.moveToFirst()){
   company=companyCur.getString(companyCur.getColumnIndex(CommonDataKinds.Organization.COMPANY));
  }
 companyCur.close();
 
 // 根据contact_ID取得HomePhone号码
 Cursor homePhoneCur = cr.query(ContactsContract.Data.CONTENT_URI,
   new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER},
        ContactsContract.Data.CONTACT_ID + "=?" + " AND "
          + ContactsContract.Data.MIMETYPE + "=? "+" AND "
          +ContactsContract.CommonDataKinds.Phone.TYPE + "=?",
        new String[]{android_id,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_HOME)}, null);
 if(homePhoneCur.moveToFirst()){
  homePhone=homePhoneCur.getString(homePhoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
 }
 homePhoneCur.close();
 
 // 根据contact_ID取得WorkPhone号码
 Cursor workPhoneCur = cr.query(ContactsContract.Data.CONTENT_URI,
   new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER},
        ContactsContract.Data.CONTACT_ID + "=?" + " AND "
          + ContactsContract.Data.MIMETYPE + "=? "+" AND "
          +ContactsContract.CommonDataKinds.Phone.TYPE + "=?",
        new String[]{android_id,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_WORK)}, null);
 if(workPhoneCur.moveToFirst()){
  workPhone=workPhoneCur.getString(workPhoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
 }
 workPhoneCur.close();
 
 // 根据contact_ID取得MobilePhone号码
 Cursor mobilePhoneCur = cr.query(ContactsContract.Data.CONTENT_URI,
   new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER},
        ContactsContract.Data.CONTACT_ID + "=?" + " AND "
          + ContactsContract.Data.MIMETYPE + "=? "+" AND "
          +ContactsContract.CommonDataKinds.Phone.TYPE + "=?",
        new String[]{android_id,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)}, null);
 if(mobilePhoneCur.moveToFirst()){
  mobilePhone=mobilePhoneCur.getString(mobilePhoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
 }
 mobilePhoneCur.close();
 
 // 根据contact_ID取得MobilePhone号码
 Cursor faxPhoneCur = cr.query(ContactsContract.Data.CONTENT_URI,
   new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER},
        ContactsContract.Data.CONTACT_ID + "=?" + " AND "
          + ContactsContract.Data.MIMETYPE + "=? "+" AND "
          +ContactsContract.CommonDataKinds.Phone.TYPE + "=?",
        new String[]{android_id,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME)}, null);
 if(faxPhoneCur.moveToFirst()){
  faxPhone=faxPhoneCur.getString(faxPhoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
 }
 faxPhoneCur.close();
 
 // 根据contact_ID取得MobilePhone号码
 Cursor emailCur = cr.query(ContactsContract.Data.CONTENT_URI,
   new String[] {ContactsContract.CommonDataKinds.Email.DATA},
        ContactsContract.Data.CONTACT_ID + "=?" + " AND "
          + ContactsContract.Data.MIMETYPE + "=? "+" AND "
          +ContactsContract.CommonDataKinds.Phone.TYPE + "=?",
        new String[]{android_id,ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,String.valueOf(ContactsContract.CommonDataKinds.Email.TYPE_WORK)}, null);
 if(emailCur.moveToFirst()){
  email=emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
 }
 emailCur.close();
 
 // 根据contact_ID取得MobilePhone号码
 Cursor addressCur = cr.query(ContactsContract.Data.CONTENT_URI,
   new String[] {ContactsContract.CommonDataKinds.StructuredPostal.CITY,ContactsContract.CommonDataKinds.StructuredPostal.REGION,
   ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY},
        ContactsContract.Data.CONTACT_ID + "=?" + " AND "补充:移动开发 , Android ,

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,