Android项目——读取手机联系人信息

加入读取联系人信息的权限
"android.permission.READ_CONTACTS"/>
content://com.android.contacts/contacts 操作的数据是联系人信息Uri
content://com.android.contacts/data/phones 联系人电话Uri
content://com.android.contacts/data/emails 联系人Email Uri
读取联系人信息
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, 
    null, null, null, null); 
  while (cursor.moveToNext()) { 
   String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
   String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
  
   Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
        null, 
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, 
        null, null); 
    while (phones.moveToNext()) { 
     String phoneNumber = phones.getString(phones.getColumnIndex( 
         ContactsContract.CommonDataKinds.Phone.NUMBER)); 
     Log.i("RongActivity", "phoneNumber="+phoneNumber);
    } 
    phones.close(); 
  
    Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
       null, 
       ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, 
       null, null); 
       while (emails.moveToNext()) { 
        // This would allow you get several email addresses 
        String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
        Log.i("RongActivity", "emailAddress="+ emailAddress);
       } 
       emails.close(); 
  } 
  cursor.close();
---------------------------------

/**

 * 读取联系人信息

 * @author Administrator yangchao

 *

 */

public class MainActivity extends Activity

{

    public static final String TAG = "yangchao";

    private ListView lvContent;

 

    @Override

    protected void onCreate(Bundle savedInstanceState)

    {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

       lvContent = (ListView) findViewById(R.id.lvContent);

      

       List> data = new ContactService(this).getInfo();

      

       SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,

               new String[]{"name", "number", "email"}, new int[]{R.id.tvName, R.id.tvNumber, R.id.tvEmail});

        lvContent.setAdapter(adapter);

    }

}

 ---------------------------------------------------------------------------------------------------------------

public class ContactService

{

    private Context context;

    public ContactService(Context context)

    {

       this.context = context;

    }

   

    public List> getInfo()

    {

       List> data = new ArrayList>();

 

       ContentResolver contentResolver = context.getContentResolver();

       Cursor cursor = contentResolver.query(

              ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

       while (cursor.moveToNext())

       {

           String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

           String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

          

           HashMap map = new HashMap();

           map.put("name", name);

          

           Cursor phones = contentResolver.query(

                  ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,

                  ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "

                         + contactId, null, null);

           while (phones.moveToNext())

           {

              String phoneNumber = phones

                     .getString(phones

                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

              map.put("number", phoneNumber);

             

              Log.i("RongActivity", "phoneNumber=" + phoneNumber);

           }

           phones.close();

 

           Cursor emails = contentResolver.query(

                  ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,

                  ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = "

                         + contactId, null, null);

           while (emails.moveToNext())

           {

              // This would allow you get several email addresses

              String emailAddress = emails

                     .getString(emails

                            .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

              map.put("email", emailAddress);

             

             

              Log.i("RongActivity", "emailAddress=" + emailAddress);

           }

          

           data.add(map);

           emails.close();

       }

       cursor.close();

       return data;

    }

}

 


你可能感兴趣的:(Android)