这是我的代码
if(Integer.parseInt(hasPhoneNumber) > 0)
{
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, contactId);
Uri myPhoneUri = Uri.withAppendedPath(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, contactId);
// Query the table
Cursor phoneCursor = managedQuery(
myPhoneUri, null, null, null, null);
// Get the phone numbers from the contact
ContentResolver cr = getContentResolver();
Cursor cursor1 = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cursor1.moveToFirst()) {
String contactId1 = cursor1.getString(
cursor.getColumnIndex(ContactsContract.Contacts._ID));
// Get all phone numbers.
Cursor phones = cr.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + contactId1, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
Toast.makeText(this, "Phone = " + number, Toast.LENGTH_LONG).show();
}
phones.close();
}
}
一个联系人一次只能获取一个电话号码。我需要同时检索多个数字,如TYPE_HOME,TYPE_MOBILE,TYPE_WORK,这些数字保存在一个联系人中,我该怎么做?
答案 0 :(得分:2)
尝试这个为我工作。
String [] cname;
int cnt=0;
boolean [] checkall;
String number1="";
String num="";
String nameofuser=ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC";
Cursor phones =getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, nameofuser);
cname=new String[phones.getCount()];
checkall=new boolean[phones.getCount()];
while(phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String emailid=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
Cursor phone1 = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (phone1.moveToFirst())
{
do
{
number1=phones.getString(phones.getColumnIndex(Phone.NUMBER));
int type=phones.getInt(phones.getColumnIndex(Phone.TYPE));
Log.d("before switch", ""+number1 );
if(num.contains(number))
{
switch (type)
{
case Phone.TYPE_HOME:
num+="\nHOME:"+number1;
break;
case Phone.TYPE_MOBILE:
num+="\nMOBILE:"+number1;
break;
case Phone.TYPE_WORK:
num+="\nWORK:"+number1;
break;
default:
break;
}
}
}while(phone1.moveToNext());
}
// phones.close();
Cursor emailCur = getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{emailid}, null);
emailCur.moveToFirst();
String email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
cname[cnt]=name+"\n"+number1+"\n"+email+num;
checkall[cnt]=true;
cnt++;
num="";
//Toast.makeText(DisplaycontectActivity.this, "\nname=\t"+ name+ "\nnum=\t"+ number+"\nmail_id\t"+email, 1).show();
}
phones.close();
AlertDialog.Builder ad=new AlertDialog.Builder(DisplaycontectActivity.this);
ad.setTitle("contact list");
ad.setMultiChoiceItems(cname, checkall, new OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
}
});
AlertDialog adb=ad.create();
adb.show();
}}
这将显示手机中存储的所有联系人详细信息以及显示地址,电话号码和EMailid。
所有竞赛显示在警告对话框中,默认情况下选择........尝试...
答案 1 :(得分:0)
String id , name;
ContentResolver cr = getContentResolver();
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, sortOrder);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString( cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.i(tag, "Id is "+ id+"\t Name is"+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);
// this second loop will retrieve all the contact numbers for a paricular contact id
while (pCur.moveToNext()) {
// Do something with phones
int phNumber = pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);
String phn = pCur.getString(phNumber);
Log.i("phn number", phn);
}
pCur.close();
}
}
}