如何从联系人获取电子邮件

时间:2018-10-01 12:57:08

标签: android android-contacts android-contentresolver

此代码可以很好地获取姓名和电话,但我不知道如何从此代码获取电子邮件。这是我的代码:

public static ArrayList<ContentValues> getContactDetails(final Context mContext){
ArrayList<ContentValues> contactList = new ArrayList<ContentValues>();


String order = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC";
Cursor managedCursor = mContext.getContentResolver().query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
        null, order);

int _number = managedCursor
        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int _name = managedCursor
        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int _id = managedCursor
        .getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);



while (managedCursor.moveToNext()) {

    ContentValues values = new ContentValues();
    Contact mContact = new Contact();


        values.put(ContactClass.CONTACT_NAME, managedCursor.getString(_name));
        values.put(ContactClass.CONTACT_MOBILE_NUMBER, managedCursor.getString(_number).replaceAll("\\s+",""));
        mContact.setPhNo(managedCursor.getString(_number).replaceAll("\\s+",""));
        mContact.setName(managedCursor.getString(_name));

        contactList.add(values);
        serverContactList.add(mContact);

    }
}

return contactList;

}

在这里我要获取电子邮件并添加到serverContactList列表中。

2 个答案:

答案 0 :(得分:0)

我编辑了您的代码,将其余代码添加到2个待办事项中。

public static ArrayList<ContentValues> getContactDetails(final Context mContext) { 
    // todo rest of things

    int _id = managedCursor
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);

    while (managedCursor.moveToNext()) {
        // we will get emails for a contact id
        String id = managedCursor.getString(_id);
        Cursor cur1 = mContext.getContentResolver().query(
                ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                new String[]{id}, null);
        if (cur1 != 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();

        // todo rest of things
    }
    return contactList;
}

答案 1 :(得分:0)

  1. 永远不要使用<form method="POST" action="<?php $_SERVER['PHP_SELF'];?>"> <input type="text" name="name" id="name"> <span id="namenotif" style="color:red;"> <span> <br> <input type="text" name="price" id="price"> <span id="pricenotif" style="color:red;"> <span> <br> <input type="submit" name="submit" id="save"><br> </form> <script> $(document).ready(function() { $(document).on("click","#save",function(){ var name = $("#name").val(); var price = $("#price").val(); if(name==""){ $("#namenotif").html("Enter a name"); $("#name").show("fast"); $("#save").show("fast"); } else if(price==""){ $("#pricenotif").html("Enter a price"); $("#price").show("fast"); $("#save").show("fast"); }else{ $.ajax({ url:"addproduct.php", type:"POST", data:{name:name,price:price}, success:function(data){ alert("Successful"); } }); } }); }); </script>
  2. 通过Data.CONTENT_URI而不是Phone.CONTENT_URI进行查询,以访问所有数据项(包括Phone和Email以及其他必要项)
  3. 通过MIMETYPE将查询限制为仅需要的内容


managedCursor