如何在活动中获取Android手机上的所有联系人?
答案 0 :(得分:3)
ContentResolver()
ArrayList<Contact>
ContactArrayAdapter
ContactArrayAdapter示例
////////////////////////////////////////////////////////////////////////////
private class ContactsAdapter extends ArrayAdapter<Contact> {
private final int _resourceId;
private final LayoutInflater _inflater;
private final Context _context;
public ContactsAdapter(Context context, int textViewResourceId, List<Contact> contacts) {
super(context, textViewResourceId, contacts);
_context = context;
_resourceId = textViewResourceId;
_inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View v, ViewGroup parent) {
ViewHolder holder;
if (v == null) {
v = _inflater.inflate(_resourceId, null);
holder = new ViewHolder();
holder.tv_name = (TextView) v.findViewById(R.id.tv_name);
holder.tv_phonenumber = (TextView) v.findViewById(R.id.tv_phonenumber);
holder.iv_photoid = (ImageView) v.findViewById(R.id.iv_photo);
v.setTag(holder);
}
else {
holder = (ViewHolder) v.getTag();
}
Contact contact = getItem(position);
holder.tv_name.setText(contact.getName());
holder.tv_phonenumber.setText(contact.getPhoneNumber());
Bitmap bm = openPhoto(contact.getPhotoId());
if (bm != null) {
holder.iv_photoid.setImageBitmap(bm);
}
else {
holder.iv_photoid.setImageDrawable(getResources()
.getDrawable(R.drawable.ic_item_profile_medium));
}
return v;
}
private Bitmap openPhoto(long contactId) {
Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
InputStream input = ContactsContract.Contacts
.openContactPhotoInputStream(_context.getContentResolver(),
contactUri);
if (input != null) {
return BitmapFactory.decodeStream(input);
}
return null;
}
private class ViewHolder {
ImageView iv_photoid;
TextView tv_name;
TextView tv_phonenumber;
}
}
将适配器分配到活动的ListView
或onCreate()
中的onResume()
。
ListView contactList = (ListView) findViewById(R.id.list_contact);
ContactsAdapter adapter = new ContactsAdapter(YourActivity.this,
R.layout.list_contact, getContactList());
contactList.setAdapter(adapter);
我通过谷歌搜索找到了上述来源,但我不记得参考网站在哪里。对不起
答案 1 :(得分:0)
使用以下代码使用 ContentResolver
获取所有联系人。 Reference Link
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(NativeContentProvider.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
}
pCur.close();
}
}
}