以下是我从手机数据库获取用户数据的代码:
Uri contactDataUri = ContactsContract.Data.CONTENT_URI;
/*final String[] projection = {
ContactsContract.Contacts._ID,
Build.VERSION.SDK_INT
>= Build.VERSION_CODES.HONEYCOMB ?
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY :
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.LOOKUP_KEY
};*/
final String selection = ContactsContract.Data.CONTACT_ID + " = " + id;
Cursor contactCursor = cr.query(
contactDataUri,
null,
selection,
null,
null
);
//contactCursor.moveToFirst();
if (contactCursor.moveToFirst()) {
contactName = contactCursor.getString(contactCursor
.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
//Getting phone numbers
if (contactCursor.getString(contactCursor.getColumnIndex("mimetype"))
.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
Toast.makeText(context, "contact id " + id + "has phones", Toast.LENGTH_SHORT).show();
switch (contactCursor.getInt(contactCursor.getColumnIndex("data2"))) {
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
homePhone = contactCursor.getString(contactCursor.getColumnIndex("data1"));
Toast.makeText(context, "contact id " + id +"has homePhone", Toast.LENGTH_SHORT).show();
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
mobilePhone = contactCursor.getString(contactCursor.getColumnIndex("data1"));
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
workPhone = contactCursor.getString(contactCursor.getColumnIndex("data1"));
break;
}
}
}
contactCursor.close();
TextView cname = (TextView) findViewById(R.id.profile_name);
cname.setText(contactName);
TextView cnum = (TextView) findViewById(R.id.profile_number);
if (!homePhone.equals("") && homePhone != null) {
cnum.setText(homePhone);
number = homePhone;
} else if (mobilePhone != null && !mobilePhone.equals("")) {
cnum.setText(mobilePhone);
number = mobilePhone;
} else if (workPhone != null && !workPhone.equals("")) {
cnum.setText(workPhone);
number = workPhone;
} else {
cnum.setText("No number found!");
contact_call = (ImageView) findViewById(R.id.profile_call);
contact_call.setVisibility(View.INVISIBLE);
contact_call.setClickable(false);
}
我正在获取contactName的信息,但是我在获取联系人存储的电话号码信息方面遇到了麻烦。 烤面包后我才知道IF条件contactCursor.getString(contactCursor.getColumnIndex(“mimetype”)) .equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 是我遇到麻烦的地方。 我需要从获取的id中获取联系人的号码。所以任何其他获得电话号码的方法都将受到赞赏。感谢。