如何在Android中获取联系信息?

时间:2012-06-14 11:22:31

标签: android contactscontract

我希望使用android从我的手机获取联系人图片,姓名,号码。我使用ContactsContract来获取这些信息。但我不能。任何人都可以知道学习的确切教程。我想在自定义列表视图中列出这些信息。提前致谢。我希望你的宝贵指导对我有所帮助。

PS:在DB我有我的朋友号码。我想使用匹配的手机号码同步手机联系人和数据库联系人。我需要在列表视图中显示所有匹配的联系人。列表视图有imageView,名称,编号..

1 个答案:

答案 0 :(得分:0)

setReminder.numbers.clear();
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
    ((Activity) mcontext).startActivityForResult(intent, 1);

和活动结果执行此操作

if (requestCode == 1 && resultCode == Activity.RESULT_OK)
  {         
        getContactInfo(intent);         
      }
} 

protected void getContactInfo(Intent intent)
{
    String phoneNumber = "";
    //String email="";

   Cursor cursor =  ((Activity) mcontext).managedQuery(intent.getData(), null, null, null, null);      
   while (cursor.moveToNext()) 
   {           
           String contactId = cursor.getString(cursor.getColumnIndex(BaseColumns._ID));

           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 = ((Activity) mcontext).getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
        //Cursor Email = ((Activity) mcontext).getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = "+ contactId,null, null);
        while (phones.moveToNext()) 
        {
          phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
          numbers.add(phoneNumber);
        }

        phones.close();
        Cursor emailCur = ((Activity) mcontext).getContentResolver().query( 
                ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
                null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = "+ contactId, 
                null, null); 


            while (emailCur.moveToNext()) { 
                // This would allow you get several email addresses
                    // if the email addresses were stored in an array
                 email = emailCur.getString(
                              emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                String emailType = emailCur.getString(
                              emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 
            } 
            emailCur.close();

            if(email==null)
            {
                email="";
            }
       }

      }          
   if(phoneNumber != null && phoneNumber.length() > 0){

       chooseContactArray.clear();
       for(int i=0;i<numbers.size();i++)
       {
           chooseContactArray.add(numbers.get(i));
       }
       adapter.notifyDataSetChanged();
   }
   cursor.close();

}