1

我是 android 新手,每次我使用以下代码时,我都想在我的联系人列表中获取现有联系人的电子邮件,但我无法获得确切的联系人电子邮件,而是任何随机电子邮件。请帮助我更正此代码,我将不胜感激。

 public void fetchingContacts(Context context) {

            Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            ContentResolver cr = context.getContentResolver();
            Cursor crsr = cr.query(uri, null, null, null, null);

            crsr.moveToFirst();

            if (crsr.getCount() > 0) {
                while (crsr.moveToNext()) {
                    ID = crsr
                            .getString(crsr
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
                    id = crsr.getString(crsr.getColumnIndex(BaseColumns._ID));
                    NAME = crsr
                            .getString(crsr
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    NUMBER = crsr
                            .getString(crsr
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                    arylist_contact_id.add(ID);
                    arylist_all_number.add(NUMBER);
                    arylist_all_names.add(NAME);
                    // ////////////



                    Cursor emailCur = cr.query(
                            ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                            null, ContactsContract.CommonDataKinds.Email.CONTACT_ID
                                    + " = " + id, null, null);

                    while (emailCur.moveToNext()) {
                        email = emailCur
                                .getString(emailCur
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                        Log.v("fetchingEMAIL ---  ", email);

                    }
                    emailCur.close();


                    Log.v("IN do while :: name and number ", ID + " , id :" + id
                            + "  " + NAME + " : " + NUMBER + "  :  " + email);
                } 

            }
            crsr.close();
        }
4

2 回答 2

1
            Cursor emailCur = cr.query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID
                            + " = ?", new String[] { id }, null);
            while (emailCur.moveToNext()) {
            String  email = emailCur
                        .getString(emailCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));

            }
            emailCur.close();

以上是帮助我的朋友正确获取电子邮件的查询,它对我有用...希望尝试一下。

于 2013-01-03T06:24:05.163 回答
0

试试这个

      Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor cur1 = cr.query( 
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
                            new String[]{id}, null); 
            while (cur1.moveToNext()) { 
                //to get the contact names
                String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                Log.e("Name :", name);
                String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                Log.e("Email", email);

                }
            } 
            cur1.close();
        }
于 2013-01-03T06:29:30.850 回答