How to get Contact Information in Android?

别等时光非礼了梦想. 提交于 2019-12-13 06:16:09

问题


I want to get Contact Image, Name, Number from my mobile using android. im using ContactsContract to fetch these information. but i can't. can anyone know the exact tutorial for learning. i want to list these info in a custom listview. Thanks in Advance. i hope your valuable guiding will help me.

PS: In the DB i have my friends number. i want to synchronize the phone contact and the DB contact using the matched phone no. and i need to display all the matched contacts in the listview. the listview have imageView, name, number..


回答1:


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

and on activity result do this

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();

}


来源:https://stackoverflow.com/questions/11032030/how-to-get-contact-information-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!