1

嗨,我正在我的应用程序中尝试此代码,我可以获取联系人列表,但是当我按下联系人姓名时,我的编辑文本中没有任何内容,我希望得到联系人电话号码,抱歉我的英语不好

         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");
        }
    }
4

4 回答 4

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));

}
于 2012-09-01T10:42:58.177 回答
4

你实际上真的很接近 - 一些事情。

  • 您的 switch 语句基于 resultCode,而不是请求代码。改变它 - 它应该是“switch(requestCode)”

  • Phone.DATA 可以工作,但为了便于阅读,请改用 Phone.NUMBER :)

  • 确保您在您的 AndroidManifest.xml 文件中设置了 android.permission.READ_CONTACTS 的权限,作为清单元素中的一个元素,而不是在应用程序元素中。该行应如下所示。

    <uses-permission android:name="android.permission.READ_CONTACTS" />

我对您的示例进行了这些修改,并获得了工作代码。

于 2011-02-22T23:42:04.980 回答
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) + "]");
}
于 2013-05-14T08:19:36.680 回答
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();
于 2011-02-23T01:33:39.473 回答