我正在尝试从个人资料中获取用户的信息(名字、姓氏、电话、电子邮件等)。下面是我的代码,奇怪的是有几条记录(cursor.moveToNext()
工作多次),其中一条包含电话值中的电子邮件,另一条包含电子邮件字段中的地址。我只需要一张正确的唱片,而不是挑选和组合碎片。
我确实添加了所需的权限,所以那里不应该有问题。
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
任何提示?谢谢
下面是我的代码
private static final String[] PERSONAL_ADDRESS_PROJ = {
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Email.ADDRESS,
};
private static final int INDEX_GIVEN_NAME = 0;
private static final int INDEX_FAMILY_NAME = INDEX_GIVEN_NAME + 1;
private static final int INDEX_PHONE_NUMBER = INDEX_FAMILY_NAME + 1;
private static final int INDEX_EMAIL = INDEX_PHONE_NUMBER + 1;
private SignupModel searchForProfile() {
final Uri uri = Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
String selection = ContactsContract.Contacts.Data.MIMETYPE + " = ?" + " OR " +
ContactsContract.Contacts.Data.MIMETYPE + " = ?" + " OR " +
ContactsContract.Contacts.Data.MIMETYPE + " = ?" ;
String [] args = new String[]{ ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE};
Cursor cursor = mContext.getContentResolver().query(uri, PERSONAL_ADDRESS_PROJ, selection, args, null);
SignupModel model;
if (cursor != null && cursor.moveToFirst()) {
try {
do
{
final String givenName = cursor.getString(INDEX_GIVEN_NAME);
final String familyName = cursor.getString(INDEX_FAMILY_NAME);
final String phone = cursor.getString(INDEX_PHONE_NUMBER);
final String email = cursor.getString(INDEX_EMAIL);
model = new SignupModel(email,null,null,givenName, familyName, phone,null);
}while (cursor.moveToNext()); //loop for debugging purpose
} catch (Exception e) {
model = null;
} finally {
cursor.close();
}
} else {
LogUtils.LOGD(TAG, "Couldn't find profile entry");
model = null;
}
return model;
}