我正在尝试从联系人列表中获取名称,联系号码和电子邮件ID并存储在阵列列表中并转发到服务器。我将姓名,电话和电子邮件ID存储到contactNameArrayList
,contactNumberArrayList
和contactEmailIdArrayList
。
下面的代码我得到了姓名,电子邮件和联系方式,但是如何将特定的电子邮件ID存储到特定联系人号?
private void getAllContacts() {
ContentResolver contactResolver = context.getContentResolver();
Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if(cursor.getCount()>0)
while ( cursor.moveToNext()) {
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Log.e("TAG", " Name: " + displayName+"==>phone Number==>"+number);
contactNameArrayList.add(displayName);
contactNumberArrayList.add(number);
final String[] projection = new String[]{ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Email.TYPE};
Cursor emailCursor = contactResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,null, null, null);
while (emailCursor.moveToNext()) {
String email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
int type = emailCursor.getInt(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
String s = (String) ContactsContract.CommonDataKinds.Email.getTypeLabel(context.getResources(), type, "");
Log.e(TAG,"EMAIL==>"+email);
}
emailCursor.close();
}
cursor.close();
Log.e(TAG,"contactNameArrayList sIZE==>"+contactNameArrayList.size()+"contactNumberArrayList size==>"+contactNumberArrayList.size()+"contactEmailAddressArrayList size==>"+contactEmailAddressArrayList.size());
}
上面的代码我调用getAllcontact
方法。我在contactNameArrayList
和contactNumberArrayList
以及contactEmailAddressList
中获得了号码和姓名以及电子邮件ID和商店。但是当我收到电子邮件时,ID响应是这样的:
姓名:efg ==>电话号码==>(256)655-555
EMAIL ==> abc@gmail.com
EMAIL ==> pqr@gmail.com
EMAIL ==> xyz@gmail.com
姓名:abc ==>电话号码==>(256)655-555
EMAIL ==> abc@gmail.com
EMAIL ==> pqr@gmail.com
EMAIL ==> xyz@gmail.com
姓名:pqr ==>电话号码==>(987)456-1238
EMAIL ==> abc@gmail.com
EMAIL ==> pqr@gmail.com
EMAIL ==> xyz@gmail.com
姓名:xyz ==>电话号码==>(987)456-1238
EMAIL ==> abc@gmail.com
EMAIL ==> pqr@gmail.com
EMAIL ==> xyz@gmail.com
那么如何获取特定手机的电子邮件ID并存储在contactEmailArrayList
中。如上所述,响应conatactNameArrayList
和contactNumberArrayList
大小相同,但ContactEmailAddressArrayList
大小不同。所以请给我一些建议。
答案 0 :(得分:0)
制作一个课程,以获取电子邮件,姓名等等。
public class UserContact {
private String userName,userEmail;
private boolean isSelect;
public boolean isSelect() {
return isSelect;
}
public void setSelect(boolean select) {
isSelect = select;
}
public UserContact(){}
public UserContact(String userName, String userEmail) {
this.userName = userName;
this.userEmail = userEmail;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
}
然后在主要活动中将一个列表作为全局级别。
private List<UserContact> mContactList = new ArrayList<>();
然后调用getContact详细信息。
/**
* this method read device in contact name and email.
*/
public void getContact() {
if (PermissionManager.checkPermission(getActivity(), READ_CONTACT)) {
mContactList.clear();
List<UserContact> contactList = new ArrayList<>();
Cursor cur = getActivity().getContentResolver().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 = getActivity().getContentResolver().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);
if (!TextUtils.isEmpty(email)) {
contactList.add(new UserContact(name, email));
}
}
cur1.close();
}
}
mContactList = contactList;
setAdapter();
} else {
PermissionManager.askForPermission(READ_CONTACT, getActivity());
}
}
上述方法仅获取姓名和电子邮件,您可以修改自己的需求并添加电话号码。