当我将代码从行"// Using the contact ID now we will get contact phone number"
开始到行"cursorPhone.close"
时,它不会在我的s3或注释3上显示任何联系信息,但会在我的asus平板电脑上显示。如果我取出上面提到的行之间的代码,代码在s3和注3上工作。我做错了什么?日志中没有错误。
private void getContacts() {
try {
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
ContactsContract.Contacts._ID };
mCursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
projection, ContactsContract.Contacts.HAS_PHONE_NUMBER + "=?", new String[] { "1" },
ContactsContract.Contacts.DISPLAY_NAME);
String phoneNumber = '';
while (mCursor.moveToNext()) {
contact contact = new contact();
String contactId = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID));
contact.setContactName(mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
// Using the contact ID now we will get contact phone number
Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
new String[]{contactId},
null);
if (cursorPhone.moveToFirst()) {
phoneNumber = (cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
cursorPhone.close();//till here
contact_list.add(contact);
}
isChecked = new boolean[mCursor.getCount()];
for (int i = 0; i < isChecked.length; i++) {
isChecked[i] = false;
}
this.mContactAdapter = new contactAdapter(this, R.layout.contactlistview, contact_list);
lv.setAdapter(this.mContactAdapter);
mCursor.close();
runOnUiThread(returnRes);
} catch (Exception e) {
Log.d("getContacts", e.getMessage());
}
}
答案 0 :(得分:0)
为什么不尝试使用 -
打开内置的联系活动Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT); Hope this helps you.
您需要在Activity的Activity Result Method中处理代码,如下所示:
if (reqCode == PICK_CONTACT && resultCode == Activity.RESULT_OK) {
// final String phoneNumber = data.getStringExtra("android.intent.extra.PHONE_NUMBER");
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Cursor c = null;
try {
c = getContentResolver().query(uri, new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
null, null, null);
if (c != null && c.moveToFirst()) {
String number = c.getString(0);
number=number.replaceAll("\\s", "");
number=number.replaceAll("-", "");
edtPhoneNumber.setText(number);
}
} finally {
if (c != null) {
c.close();
}
}
}
}
finishActivity(PICK_CONTACT);
}
if (reqCode == PICK_CALLLOG && resultCode == Activity.RESULT_OK) {
edtPhoneNumber.setText(data.getStringExtra("CallerNumber"));
}
if (reqCode == PICK_SMSLOG && resultCode == Activity.RESULT_OK) {
edtPhoneNumber.setText(data.getStringExtra("CallerNumber"));
}
}