使用电子邮件 ID 获取联系人
Posted
技术标签:
【中文标题】使用电子邮件 ID 获取联系人【英文标题】:Get contacts with email id 【发布时间】:2011-07-12 15:40:26 【问题描述】:我需要通过电子邮件获取联系人信息(光标)。它们必须是不同的。如果他收到电子邮件,则每个联系人必须有一个条目。怎么做?我的目标是 2.0 附带的新联系人 API。
1)我尝试使用 CursorJoiner 来做,但是发生了一件奇怪的事情。这是我的代码:
MatrixCursor matCur = new MatrixCursor(
new String[]
Contacts._ID,
Contacts.DISPLAY_NAME,
"photo_id",
"starred"
);
Cursor newContactCursor = managedQuery(
ContactsContract.Contacts.CONTENT_URI,
new String[]
Contacts._ID,
Contacts.DISPLAY_NAME,
"photo_id",
"starred"
,
null,
null,
null//Contacts._ID
);
newContactCursor.moveToFirst();
Cursor emailCur = managedQuery(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]
Email.CONTACT_ID,
Email.DATA1
,
null,
null,
Email.CONTACT_ID
);
emailCur.moveToFirst();
CursorJoiner joiner = new CursorJoiner(
newContactCursor,
new String[]Contacts._ID,
emailCur,
new String[] Email.CONTACT_ID
);
for (CursorJoiner.Result joinerResult : joiner)
switch (joinerResult)
case LEFT:
// handle case where a row in cursorA is unique
//Log.i(TAG,"L|"+
//newContactCursor.getString(newContactCursor.getColumnIndex("_id")) );
break;
case RIGHT:
// handle case where a row in cursorB is unique
//Log.i(TAG,
//"R|"+
//emailCur.getString(emailCur.getColumnIndex("contact_id")) );
break;
case BOTH:
//Log.i(TAG,
//"L|"+
//newContactCursor.getString(newContactCursor.getColumnIndex("_id"))+
//"|R|"+
//emailCur.getString(emailCur.getColumnIndex("contact_id")) );
Log.i(TAG, newContactCursor.getString(newContactCursor.getColumnIndex("_id"))+"|"+
newContactCursor.getString(newContactCursor.getColumnIndex("display_name"))+"|"+
emailCur.getString(emailCur.getColumnIndex(Email.DATA1)));
String[] columnValues =
newContactCursor.getString(newContactCursor.getColumnIndex("_id")),
newContactCursor.getString(newContactCursor.getColumnIndex("display_name")),
newContactCursor.getString(newContactCursor.getColumnIndex("photo_id")),
newContactCursor.getString(newContactCursor.getColumnIndex("starred"))
;
matCur.addRow(columnValues);
break;
现在我的问题是我得到了这样的输出: 在这个日志中它的_id |显示名称 |电子邮件ID 由于隐私问题,我已经更换了它们
1|[contact name]|[email id]
4|[contact name]|[email id]
5|[contact name]|[email id]
6|[contact name]|[email id]
7|
8|
9|
90|
91|
92|
93|
94|
95|
96|
97|
98|
99|
但是你可以看到它直接从9跳到90然后都是9 9 9,这是什么?
2) 我们可以使用 distinct 关键字来做到这一点吗?是否可以使用 ContactsContract 等联系人提供者?
【问题讨论】:
任何人都可以添加标签 CursorJoiner 吗? 【参考方案1】:尝试使用这个 sn-p: 在列表视图的同一行中显示联系人姓名和电子邮件。
/**
* Populate the contact list based on account.
*/
private void populateContactList()
// Build adapter with contact entries
Cursor cursorEmail = getContactsEmail();//get all emails
String[] fields = new String[] //fields of data to take
ContactsContract.Contacts._ID,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA
;
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, R.layout.contact_entry, cursorEmail ,
fields, new int[]
R.id.UID,R.id.contactEntryText,R.id.contactEmail);
mContactList.setAdapter(adapter);
/**
* Obtains the contact list for the currently selected account.
*
* @return A cursor for for accessing the contact list.
*/
private Cursor getContactsEmail()
// Run query
Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
String[] projection = new String[]
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA
;
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP +"='1'";
//showing only visible contacts
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
【讨论】:
我认为如果他有多个电子邮件 ID,这将显示同一联系人的多个条目。我对吗?我需要与众不同。 所以有什么解决方法吗?我需要收到电子邮件的联系人姓名 集合不允许重复键。【参考方案2】:我刚刚遇到了同样的问题。我知道这个帖子很老了,但也许这个答案会在未来对其他人有所帮助。
您必须按 MIME 类型过滤以去除重复项。我就是这样做的:
Uri contacts = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[]
ContactsContract.Contacts._ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.DISPLAY_NAME
;
String selection =
ContactsContract.Contacts.IN_VISIBLE_GROUP + " = ? AND " +
ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?" + " AND " +
ContactsContract.Data.MIMETYPE + "='" +
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "'";
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
mContactCursor = managedQuery(
contacts,
projection,
selection,
new String[] "1", constraint.toString() + '%',
sortOrder);
【讨论】:
我没有运行 Leon 的代码,但他的论点似乎是合法的,因此接受了他的回答,尽管现在 managedQuery 已经没有用了。【参考方案3】:用简单的方法来做 首先找到contact_id,在此基础上我们将搜索与该联系人相关的所有email_id。 在任何按钮单击事件上编写此代码
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 2);
现在是活动结果,
protected void onActivityResult(int requestCode, int resultCode, Intent data)
System.out.println("Request Code--"+requestCode);
super.onActivityResult(requestCode, resultCode, data);
if (data != null && requestCode == 2)
fromCurrent = true;
Uri uri = data.getData();
//fromCurrent=true;
if (uri != null)
Cursor c = null;
try
c = getContentResolver().query(uri, new String[]
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA ,
ContactsContract.CommonDataKinds.Email.TYPE ,
null, null, null);
if (c != null && c.moveToFirst())
String id = c.getString(0);
String name = c.getString(1);
System.out.println("id "+id+" name:"+name);
ContactID = id;
retriveEmail();
if(arrEmail.size() == 0)
showToast("No Email Address found for the selected contact!");
else
ListFile = arrEmail.toArray(new String[arrEmail.size()]);
builder1 = new AlertDialog.Builder(ShareTheHeart_Activity.this);
builder1.setTitle("Select an email address :");
builder1.setSingleChoiceItems(ListFile,-1,new DialogInterface.OnClickListener() //@Override
public void onClick(DialogInterface dialog, int which)
txtEmail.setText(ListFile[which]);
alert.cancel();
);
alert = builder1.create();
alert.show();
finally
if (c != null)
c.close();
在哪里创建retriveEmail方法,在你的代码中这样写,
private void retriveEmail()
try
arrEmail = new ArrayList<String>();
String id = ContactID;
// query for everything email
cursor = getContentResolver().query(Email.CONTENT_URI,
null, Email.CONTACT_ID + "=?", new String[] id ,
null);
int emailIdx = cursor.getColumnIndex(Email.DATA);
// let's just get the first email
if (cursor.moveToFirst())
do
email = cursor.getString(emailIdx);
arrEmail.add(email);
Log.v("ABC", "Got email: " + email);
while(cursor.moveToNext());
else
Log.w("ABC", "No results");
catch (Exception e)
Log.e("ABC", "Failed to get email data", e);
finally
if (cursor != null)
cursor.close();
就是这样。
请看懂代码,不要随便复制粘贴!
【讨论】:
以上是关于使用电子邮件 ID 获取联系人的主要内容,如果未能解决你的问题,请参考以下文章
如何从android中的单个查询中获取联系人的名字,姓氏,电子邮件ID