我正在开发一个应用程序,它将使用联系人列表的“ME”标题下的phoneOwner详细信息(如姓名,phoneNo,电子邮件ID等)。 有没有办法获得所有这些细节?
答案 0 :(得分:0)
此代码将检索所有联系人Inside ContactList
的所有contactDetailspublic class MainActivity extends Activity {
TextView textDetail;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textDetail = (TextView) findViewById(R.id.textView1);
readContacts();
}
public void readContacts() {
StringBuffer sb = new StringBuffer();
sb.append("your contacts");
ContentResolver content = getContentResolver();
Cursor c = content.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);
String phone = null;
String emailContact = null;
String emailType = null;
String name = null;
if (c.getCount() > 0) {
while (c.moveToNext()) {
String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(c.getString(c
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
System.out.println("ContactName : " + name + ", ID : " + id);
sb.append("\n Contact Name:" + name);
Cursor phoneno = content.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (phoneno.moveToNext()) {
phone = phoneno.getString(phoneno.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
sb.append("\n Phone number:" + phone);
System.out.println("phone" + phone);
}
phoneno.close();
Cursor emailid = content.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (emailid.moveToNext()) {
emailContact = emailid.getString(emailid.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
emailType = emailid.getString(emailid.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
sb.append("\nEmail:" + emailContact + "Email type:"
+ emailType);
System.out.println("Email " + emailContact
+ " Email Type : " + emailType);
}
emailid.close();
}
sb.append("\n");
sb.append("Next");
}
textDetail.setText(sb);
}
}
}
要仅检索一个联系人添加行,如下所示
if(name.equals("ME"){
// whatever you want to do
}
它仅显示该联系人详细信息..
答案 1 :(得分:0)
Cursor c = activity.getContentResolver().query(
ContactsContract.Profile.CONTENT_URI, null, null, null, null);
int count = c.getCount();
String[] columnNames = c.getColumnNames();
c.moveToFirst();
int position = c.getPosition();
if (count == 1 && position == 0) {
for (int i = 0; i < count; i++) {
for (int j = 0; j < columnNames.length; j++) {
String columnName = columnNames[j];
System.out.println(">>"+columnName+" = "+cursorContacts.getString(cursorContacts.getColumnIndex(columnName)));
}
c.moveToNext();
}
}
c.close();