Android 联系人信息的获取
public
class
MainActivity
extends
Activity {
String id, name, phoneNumber, email;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ContentResolver contentResolver =
this
.getContentResolver();
Cursor cursor = contentResolver.query(
android.provider.ContactsContract.Contacts.CONTENT_URI,
null
,
null
,
null
,
null
);
while
(cursor.moveToNext()) {
// 来获取联系人的ID和NAME
id = cursor
.getString(cursor
.getColumnIndex(android.provider.ContactsContract.Contacts._ID));
name = cursor
.getString(cursor
.getColumnIndex(android.provider.ContactsContract.Contacts.DISPLAY_NAME));
// 获取联系人的电话号码
Cursor phoneCursor = contentResolver
.query(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null
,
android.provider.ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+
"="
+ id,
null
,
null
);
while
(phoneCursor.moveToNext()) {
phoneNumber = phoneCursor
.getString(phoneCursor
.getColumnIndex(android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println(
"id="
+ id +
"name = "
+ name
+
"phoneNumber="
+ phoneNumber);
}
phoneCursor.close();
// 获取联系人的电话号码
Cursor emailCursor = contentResolver
.query(android.provider.ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null
,
android.provider.ContactsContract.CommonDataKinds.Email.CONTACT_ID
+
"="
+ id,
null
,
null
);
while
(emailCursor.moveToNext()) {
email = emailCursor
.getString(emailCursor
.getColumnIndex(android.provider.ContactsContract.CommonDataKinds.Email.DATA));
System.out.println(
"id="
+ id +
"name = "
+ name +
"email="
+ email);
}
emailCursor.close();
}
cursor.close();
}
}
|
参考:
本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2013/03/29/2989422.html,如需转载请自行联系原作者