【问题标题】:Getting user information from Contact List Android从 Android 联系人列表中获取用户信息
【发布时间】:2013-07-02 08:23:41
【问题描述】:

我需要从我的联系人列表中的每个联系人那里获取姓名、电子邮件和电话号码。

问题是我可以得到姓名和电话号码,但不能得到电子邮件。我收到的是电话号码而不是电子邮件。

这是我的代码:

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 phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            String email = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));

            ContactItem objContact = new ContactItem();
            objContact.setName(name);
            objContact.setPhoneNo(phoneNumber);
            objContact.setEmail(email);
            list.add(objContact);

        }
        phones.close();

【问题讨论】:

    标签: java android android-contacts


    【解决方案1】:
        Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
        while (cursor.moveToNext()) {
            String contactId = cursor.getString(cursor.getColumnIndex(
                    ContactsContract.Contacts._ID));
    
            //May want to get basic info here like name, phone
            //Example:
            //Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
            //while (phones.moveToNext()) {
            //    String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
            //    Log.i("phone", phoneNumber);
            //}
            //phones.close();
    
            Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
            while (emails.moveToNext()) {
                String emailAddress = emails.getString(
                        emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
    
                Log.i("emails", emailAddress);
            }
            emails.close();
        }
        cursor.close();
    

    来源和参考: How to read contacts on Android 2.0

    【讨论】:

    • 感谢分享,+1。
    【解决方案2】:

    public void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data);

        switch (reqCode) {
        case (PICK_CONTACT):
            if (resultCode == Activity.RESULT_OK) {
                String phoneNumber = null;
                String name = null;
                String emailAddress = null;
    
                Uri contactData = data.getData();
                Cursor c = managedQuery(contactData, null, null, null, null);
    
                if (c.moveToFirst()) {
                    Cursor cursor = null;
    
                    cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
                    while (cursor.moveToNext()) {
                        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                        name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                        // May want to get basic info here like name, phone
                        // Example:
                        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
                                null, null);
                        while (phones.moveToNext()) {
                            phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            Log.i("phone", phoneNumber);
                        }
                        phones.close();
    
                        Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,
                                null, null);
                        while (emails.moveToNext()) {
                            emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                            Log.i("emails", emailAddress);
                        }
                        emails.close();
                    }
                    cursor.close();
    
                    Log.i(DEBUG_TAG, "Got a contact result: " + name + emailAddress + "Phone Number is :- " + phoneNumber);
                    Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
    
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-21
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多