从ID为Android的联系人提供者检索电话号码 [英] Retrieving a Phone Number from Contacts Provider with ID, Android

查看:89
本文介绍了从ID为Android的联系人提供者检索电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够检索联系人ID,但是稍后我希望根据联系人ID分别检索电话号码.下面的代码返回电话号码的空结果.(我希望以后再检索名字和电话号码并填充一个视图,但我只是想让电话号码首先起作用.)

I am able to retrieve the contact ID, but then later I wish to separately retrieve the phone number based on the contact ID. The code below is returning a null result for the phone number. (I do wish later to retrieve the name and phone number together and populate a view, but I am just trying to get the phone number to work first).

在我的onCreate中,我有这段代码

In my onCreate I have this code

   String phoneNum = getPhoneNumber(myID);
   TextView phoneTextView = (TextView) findViewById(R.id.textViewPhone);
   phoneTextView.setText(phoneNum);

这是getPhoneNumber()的方法

This is the method for getPhoneNumber()

    protected String getPhoneNumber(String id) {
    ArrayList<String> phones = new ArrayList<String>();

    Cursor cursor = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
            new String[]{id}, null);

    while (cursor.moveToNext()) {
        phones.add(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
    }

    cursor.close();

    String phoneNum;

    phoneNum = phones.get(0);
    return phoneNum;
}//end getPhoneNumber();

}

这将产生错误java.lang.IndexOutOfBoundsException:无效的索引0,大小为0,我打算为其创建一些错误处理.但是,我仍然可以确定我具有前面代码中的ID,所以我不知道为什么ArrayList返回null.如果您想查看该代码,它也位于我的onCreate中:

This produces the error java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0, which I plan on creating some error handling for. But still, I am certain I have the ID from the previous code, so I don't know why the ArrayList returns null. If you would like to see that code, it is also in my onCreate:

    Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

            if (cursor.getCount() != 0) {
                int numContacts = cursor.getCount();

                ArrayList<String> idList = new ArrayList<>();
                Random rand = new Random();
                int randomNum = rand.nextInt(numContacts);

                while (cursor.moveToNext()) {

                    String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    idList.add(id);
                }

                myID = idList.get(randomNum);
                String myString = Integer.toString(randomNum);
                TextView myTextView = (TextView) findViewById(R.id.textViewID);
                myTextView.setText(myString);
                if (myID != null) {

                    myTextView.setText(myID);
                } else {

                    myTextView.setText("Try Again!");
                }
            } else {
                Toast.makeText(getApplicationContext(), "Your have no contacts.", Toast.LENGTH_SHORT).show();
            }
            cursor.close();

推荐答案

// You can fetch the Contact Number and Email With Following Methods.
String phone = getPhoneNumber(ContactId);
String email = getEmail("" + ContactId);

private String getPhoneNumber(long id) {
        String phone = null;
        Cursor phonesCursor = null;
        phonesCursor = queryPhoneNumbers(id);
        if (phonesCursor == null || phonesCursor.getCount() == 0) {
            // No valid number
            //signalError();
            return null;
        } else if (phonesCursor.getCount() == 1) {
            // only one number, call it.
            phone = phonesCursor.getString(phonesCursor
                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        } else {
            phonesCursor.moveToPosition(-1);
            while (phonesCursor.moveToNext()) {

                // Found super primary, call it.
                phone = phonesCursor.getString(phonesCursor
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                break;

            }
        }

        return phone;
    }

    private Cursor queryPhoneNumbers(long contactId) {
        ContentResolver cr = getContentResolver();
        Uri baseUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
                contactId);
        Uri dataUri = Uri.withAppendedPath(baseUri,
                ContactsContract.Contacts.Data.CONTENT_DIRECTORY);

        Cursor c = cr.query(dataUri, new String[]{ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.NUMBER,
                        ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY, ContactsContract.RawContacts.ACCOUNT_TYPE,
                        ContactsContract.CommonDataKinds.Phone.TYPE,
                        ContactsContract.CommonDataKinds.Phone.LABEL},
                ContactsContract.Data.MIMETYPE + "=?",
                new String[]{ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE}, null);
        if (c != null && c.moveToFirst()) {
            return c;
        }
        return null;
    }


    private String getEmail(String id) {
        String email = "";
        ContentResolver cr = getContentResolver();
        Cursor emailCur = cr.query(
                ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                new String[]{id}, null);
        while (emailCur.moveToNext()) {
            // This would allow you get several email addresses
            // if the email addresses were stored in an array
            email = emailCur.getString(
                    emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
//          String emailType = emailCur.getString(
//                  emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
        }
        emailCur.close();
        return email;
    }

这篇关于从ID为Android的联系人提供者检索电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆