我正在寻找从设备获取联系方式(电话,电子邮件,邮编)的解决方案。我已经为OS 1.6和2.2实现了代码。我正在使用API级别4(1.6)构建应用程序,但是当我在2.2设备中运行构建时,我收到电子邮件和电话字符串为空。
代码 - 适用于操作系统1.6
public void ImportCustomers(Context context) {
StringBuilder sFirstname = new StringBuilder();
StringBuilder sLastname = new StringBuilder();
StringBuilder sEmail = new StringBuilder();
StringBuilder sZip = new StringBuilder();
StringBuilder sCell = new StringBuilder();
String name, phoneNumber, email, emailid, contactDetails[];
contactDetails = new String[] { People._ID, People.NAME, People.NUMBER,
People.PRIMARY_EMAIL_ID };
Cursor contactCursor = context.getContentResolver().query(
People.CONTENT_URI, contactDetails, null, null, People.NAME);
while (contactCursor.moveToNext()) {
name = contactCursor.getString(contactCursor
.getColumnIndex(People.NAME));
phoneNumber = contactCursor.getString(contactCursor
.getColumnIndex(People.NUMBER));
emailid = contactCursor.getString(contactCursor
.getColumnIndex(People.PRIMARY_EMAIL_ID));
if (name.contains(" ")) {
sFirstname.append("|" + name.split("\\ ")[0].trim());
sLastname.append("|" + name.split("\\ ")[1].trim());
} else if (name.contains(".")) {
sFirstname.append("|" + name.split("\\.")[0].trim());
sLastname.append("|" + name.split("\\.")[1].trim());
} else if (name.contains(",")) {
sFirstname.append("|" + name.split("\\,")[1].trim());
sLastname.append("|" + name.split("\\,")[0].trim());
} else {
sFirstname.append("|" + name);
sLastname.append("|Unknown");
}
if (emailid != null) {
Cursor emailCur = context.getContentResolver().query(
Contacts.ContactMethods.CONTENT_EMAIL_URI, null,
People.PRIMARY_EMAIL_ID + " = ?",
new String[] { emailid }, null);
while (emailCur.moveToNext()) {
email = emailCur.getString(emailCur
.getColumnIndex(Contacts.ContactMethods.DATA));
sEmail.append("|" + email);
break;
}
emailCur.close();
emailCur = null;
} else
sEmail.append("|");
sCell.append("|" + (phoneNumber == null ? "" : phoneNumber));
sZip.append("|");
}
contactCursor.close();
contactCursor = null;
if (sFirstname.toString().length() == 0) {
sFirstname.append("|");
sLastname.append("|");
sCell.append("|");
sEmail.append("|");
sZip.append("|");
}
代码 - 适用于OS 2.2
private void populateContacts() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
// ID AND NAME FROM CONTACTS CONTRACTS
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.i("Pratik", "ID :" + id);
Log.i("Pratik", "NAME :" + name);
// GET PHONE NUMBERS WITH QUERY STRING
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 WE HAVE CURSOR GET THE PHONE NUMERS
while (pCur.moveToNext()) {
// Do something with phones
String phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
String phoneType = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
Log.i("Pratik", "PHONE :" + phone);
Log.i("Pratik", "PHONE TYPE :" + phoneType);
}
pCur.close();
}
//WHILE WE HAVE CURSOR GET THE EMAIL
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
String email = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
Log.i("Pratik", "EMAIL :" + email);
Log.i("Pratik", "EMAIL TYPE :" + emailType);
}
emailCur.close();
//FOR GETTTING ZIP & POSTAL
String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] addrWhereParams = new String[]{id,
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI,
null, addrWhere, addrWhereParams, null);
while(addrCur.moveToNext()) {
String poBox = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
String street = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
String city = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
String state = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
String postalCode = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
String country = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
String type = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
Log.i("Pratik", "POBOX :" + poBox);
Log.i("Pratik", "STREET :" + street);
Log.i("Pratik", "CITY :" + city);
Log.i("Pratik", "STATE :" + state);
Log.i("Pratik", "POSTALCODE :" + postalCode);
Log.i("Pratik", "COUNTRY :" + country);
Log.i("Pratik", "TYPE :" + type);
}
addrCur.close();
}
}
}
以下是我找到的代码段
public void ImportCustomer(Context context) {
int sdk = new Integer(Build.VERSION.SDK).intValue();
if (sdk >= 5) {
try {
Class clazz = Class.forName("android.provider.ContactsContract$Contacts");
CONTENT_URI = (Uri) clazz.getField("CONTENT_URI").get(clazz);
} catch (Throwable t) {
Log.e("PickDemo", "Exception when determining CONTENT_URI", t);
}
} else {
CONTENT_URI = Contacts.People.CONTENT_URI;
}
}
有人建议我如何借助此代码段获取联系人信息?
由于 Pratik Goswami
答案 0 :(得分:0)
该代码段本身对您没有多大帮助。我对此感到相当自信,因为我编写了代码片段所基于的代码。 : - )
This sample code显示使用条件类加载来获取手机上所有联系人的联系人,电子邮件和电话号码,使用1.x和2.x API。