嗨我在我的应用程序中尝试此代码我可以获取联系人列表但是当我按下联系人姓名时我没有在我的edittext中得到任何东西,我希望得到联系电话号码 抱歉我的英语很差
public void doLaunchContactPicker(View view) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode=RESULT_OK) {
case CONTACT_PICKER_RESULT:
Cursor cursor = null;
String phone = "";
try {
Bundle extras = data.getExtras();
Set<String> keys = extras.keySet();
Iterator<String> iterate = keys.iterator();
while (iterate.hasNext()) {
String key = iterate.next();
Log.v(DEBUG_TAG, key + "[" + extras.get(key) + "]");
}
Uri result = data.getData();
Log.v(DEBUG_TAG, "Got a contact result: "
+ result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
cursor = getContentResolver().query(Phone.CONTENT_URI,
null, Phone.CONTACT_ID + "=?", new String[] { id },
null);
int PhoneIdx = cursor.getColumnIndex(Phone.DATA);
if (cursor.moveToFirst()) {
phone = cursor.getString(PhoneIdx);
Log.v(DEBUG_TAG, "Got number: " + phone);
} else {
Log.w(DEBUG_TAG, "No results");
}
} catch (Exception e) {
Log.e(DEBUG_TAG, "Failed to Number", e);
} finally {
if (cursor != null) {
cursor.close();
}
EditText ponenumber = (EditText) findViewById(R.id.ednum);
ponenumber.setText(phone);
if (phone.length() == 0) {
Toast.makeText(this, "No number found for contact.",
Toast.LENGTH_LONG).show();
}
}
break;
}
} else {
Log.w(DEBUG_TAG, "Warning: activity result not ok");
}
}
答案 0 :(得分:6)
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
String Name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
String Number=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
答案 1 :(得分:4)
你真的很亲密 - 有些事情。
您的switch语句基于resultCode,而不是请求代码。改变它 - 它应该读作“switch(requestCode)”
Phone.DATA可以使用,但为了便于阅读,请使用Phone.NUMBER:)
确保您拥有AndroidManifest.xml文件中设置的android.permission.READ_CONTACTS权限,作为清单元素中的元素,但不在应用程序元素中。这条线应该是这样的。
<uses-permission android:name="android.permission.READ_CONTACTS" />
我对您的示例进行了这些修改,并获得了正常工作的代码。
答案 2 :(得分:0)
您可以更改您想要获取电话号码的代码,如下所示
List numberList = new ArrayList();
Uri baseUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,contactId);
Uri targetUri = Uri.withAppendedPath(baseUri,Contacts.Data.CONTENT_DIRECTORY);
Cursor cursor = getContentResolver().query(targetUri,
new String[] {Phone.NUMBER},Data.MIMETYPE+"='"+Phone.CONTENT_ITEM_TYPE+"'",null, null);
startManagingCursor(cursor);
while(cursor.moveToNext()){
numberList.add(cursor.getString(0));
}
cursor.close();
答案 3 :(得分:0)
这是一个老帖子,但它可以帮助某人。
如果您只需要提取联系人,您可以使用帖子的第一个代码而不包含此部分:
Bundle extras = data.getExtras();
Set<String> keys = extras.keySet();
Iterator<String> iterate = keys.iterator();
while (iterate.hasNext()) {
String key = iterate.next();
Log.v(DEBUG_TAG, key + "[" + extras.get(key) + "]");
}