【问题标题】:Android Getting Contact list with phone numbersAndroid获取带有电话号码的联系人列表
【发布时间】:2014-11-26 07:21:25
【问题描述】:

我正在创建一个需要联系人及其电话号码的应用。我可以使用以下代码获取联系人姓名列表

private Contact[] contact_read;
private Cursor mCursor;

String[] projection = new String[] { Contacts.HAS_PHONE_NUMBER,
        Contacts._ID, Contacts.DISPLAY_NAME };

mCursor = managedQuery(Contacts.CONTENT_URI, projection,
        Contacts.HAS_PHONE_NUMBER + "=?", new String[] { "1" },
        Contacts.DISPLAY_NAME);

if (mCursor != null) {
    mCursor.moveToFirst();
    contact_read = new Contact[mCursor.getCount()];

int j = 0;
    do {

        contact_read[j] = new Contact(mCursor.getString(mCursor
                .getColumnIndex(Contacts.DISPLAY_NAME)));
        j++;
    } while (mCursor.moveToNext());

} else {
    System.out.println("Cursor is NULL");
}

联系人类是

private static class Contact {
    private String name = "";

    public Contact() {
    }

    public Contact(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

但是当我添加以下代码来获取电话号码时,应用程序崩溃了

ContentResolver cr = getContentResolver();
String id = mCursor.getString(mCursor.getColumnIndex(Contacts._ID));
Cursor pCur = cr.query(Phone.CONTENT_URI,null,Phone.CONTACT_ID +" = ?", new String[]{id}, null);
pCur.moveToFirst();

int j = 0;
do {
       contact_read[j] = new Contact(pCur.getString(pCur.getColumnIndex(Phone.NUMBER)));
        j++;
} while (pCur.moveToNext());

您有什么建议需要进行哪些更改才能获取电话号码??

【问题讨论】:

标签: android contacts android-contacts


【解决方案1】:

这是从我们的手机中获取所有联系人的代码

while (phones.moveToNext())
        {
          String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
          String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
           arraylist_connect.add(new Connect(name,phoneNumber,"Mobile"));

        }

        phones.close();

【讨论】:

    猜你喜欢
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多