znl_12 2013-07-15 06:09 采纳率: 0%
浏览 2318
已采纳

如何从数据库中读取联系人信息?

我想读取所有的联系人信息到 PhoneBookBean 中。
PhoneBookBean 包含first_name, lastname, email_address,但是看起来很难读取 People.URL。
使用 ContactsContract,需要 mime_type,并且必须提供 lookupKey,我需要迭代所有的联系人一个一个人输入。
如何从数据库中读取联系人信息?

  • 写回答

1条回答

  • Baby_Bonnie 2013-07-15 09:19
    关注

    解决方案:

    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
    getContactInfo(intent);
        protected void getContactInfo(Intent intent)
        {
           Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);      
           while (cursor.moveToNext()) 
           {           
               String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
               name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
               String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
               if ( hasPhone.equalsIgnoreCase("1"))
                   hasPhone = "true";
               else
                   hasPhone = "false" ;
    
               if (Boolean.parseBoolean(hasPhone)) 
               {
                Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
                while (phones.moveToNext()) 
                {
                    phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }
                phones.close();
               }
               // Find Email Addresses
               Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,null, null);
               while (emails.moveToNext()) 
               {
                String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
               }
               emails.close();
            Cursor address = getContentResolver().query(
                        ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + contactId,
                        null, null);
            while (address.moveToNext()) 
            { 
              // These are all private class variables, don't forget to create them.
                 String poBox      = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
                 String street     = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
                 String city       = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
                 String state      = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
                 String postalCode = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
                 String country    = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
    String  type   = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
            }  //address.moveToNext()   
          }  //while (cursor.moveToNext())        
           cursor.close();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型