Android 联系人列表获取电话号码

发布于 2024-10-19 03:28:38 字数 2660 浏览 2 评论 0原文

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

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

hi i am trying this code in my app i can get the contact list but when i press on contact name i didnt get any thing in my edittext which i expected to get the contact phone number
sorry about my poor english

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吐个泡泡 2024-10-26 03:28:38
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));

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

}
落花随流水 2024-10-26 03:28:38

你们实际上非常接近——有几件事。

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

  • Phone.DATA 可以工作,但为了可读性而使用 Phone.NUMBER :)

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

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

You're actually really close- A few things.

  • Your switch statement is basing off of the resultCode, not the request code. Change that- It should read "switch(requestCode)"

  • Phone.DATA will work, but use Phone.NUMBER instead for readability sake:)

  • Make sure you have the permission for android.permission.READ_CONTACTS set in your AndroidManifest.xml file, as an element inside the manifest element, but not in the application element. The line should should look like this.

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

I made those modifications to your sample, and got working code.

一腔孤↑勇 2024-10-26 03:28:38

您可以更改您想要获取电话号码的代码,如下所示


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

you can change you code that you want get phone number,as the following


  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();
玩世 2024-10-26 03:28:38

这是一个旧帖子,但它可以帮助某人。

如果您只需要接听联系人,则可以使用帖子的第一个代码,而无需这部分:

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

It is an old post but it could help someone.

If you just have to Pick-up a contact, you can use the first code of the post without this part:

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) + "]");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文