연락처 목록 Android에서 이메일 주소 만 가져 오기
이메일 주소가있는 연락처 이름 만 표시하고 싶습니다. 그렇지 않으면 해당 연락처 이름이 목록에 표시되지 않아야합니다. 어떻게 할 수 있습니까? 아무도 나를 도와 줄 수 있습니까?
public ArrayList<String> getNameEmailDetails(){
ArrayList<String> names = new ArrayList<String>();
ContentResolver cr = getContentResolver();
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);
if(email!=null){
names.add(name);
}
}
cur1.close();
}
}
return names;
}
위의 메소드는 이메일 ID가있는 이름의 배열 목록을 반환합니다.
다음은 이메일 주소를 가져 오는 초고속 쿼리입니다. 다른 답변에서 제안한 것처럼 모든 연락처 열을 가져 오는 것보다 훨씬 빠릅니다.
public ArrayList<String> getNameEmailDetails() {
ArrayList<String> emlRecs = new ArrayList<String>();
HashSet<String> emlRecsHS = new HashSet<String>();
Context context = getActivity();
ContentResolver cr = context.getContentResolver();
String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID,
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
String order = "CASE WHEN "
+ ContactsContract.Contacts.DISPLAY_NAME
+ " NOT LIKE '%@%' THEN 1 ELSE 2 END, "
+ ContactsContract.Contacts.DISPLAY_NAME
+ ", "
+ ContactsContract.CommonDataKinds.Email.DATA
+ " COLLATE NOCASE";
String filter = ContactsContract.CommonDataKinds.Email.DATA + " NOT LIKE ''";
Cursor cur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, filter, null, order);
if (cur.moveToFirst()) {
do {
// names comes in hand sometimes
String name = cur.getString(1);
String emlAddr = cur.getString(3);
// keep unique only
if (emlRecsHS.add(emlAddr.toLowerCase())) {
emlRecs.add(emlAddr);
}
} while (cur.moveToNext());
}
cur.close();
return emlRecs;
}
'Agarwal Shankar'에서 제공 한 코드를 시도했지만 테스트 장치에서 연락처를 가져 오는 데 약 4 초가 걸렸고이 코드는 약 0.04 초가 걸렸습니다.
또 다른 해결책.
private static final Uri URI_CONTACT_DATA = ContactsContract.Data.CONTENT_URI;
private static final String COLUMN_EMAIL = ContactsContract.CommonDataKinds.Email.ADDRESS;
private static final String COLUMN_DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME_PRIMARY;
private static final String COLUMN_MIMETYPE = ContactsContract.Data.MIMETYPE;
private static final String[] PROJECTION = {
COLUMN_DISPLAY_NAME,
COLUMN_EMAIL,
COLUMN_MIMETYPE
};
private Cursor getCursor() {
ContentResolver resolver = context.getContentResolver();
String selection = COLUMN_MIMETYPE + "=?";
final String[] selectionArgs = {ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE};
return resolver.query(URI_CONTACT_DATA, PROJECTION, selection, selectionArgs, null);
}
The issue is that the table at ContactsContract.Contacts.CONTENT_URI
holds the entire contact database. This includes phone numbers, emails, organisations, and even completely custom data, so you cannot use it without filtering with ContactsContract.Data.MIMETYPE
. A row in this database holds a value (or values, it has 15 generic columns) related to a certain account, so you might need to group them yourself. I needed this to autocomplete emails, so the format (email per row) was perfect.
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Cursor emailCur = getContentResolver().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.DATA));
Log.e("Email",name+" "+email);
}
emailCur.close();
}
}
If you can already get the contacts and their email address (if it exists), why don't you just remove the contacts with no email address from your list?
See here for more information on the Android Contacts API.
Declare A Global Variable
// Hash Maps
Map<String, String> nameEmailMap = new HashMap<String, String>();
Then Use The Function Below
private void getEmailIDs() {
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null, null, null);
// Loop Through All The Emails
while (emails.moveToNext()) {
String name = emails.getString(emails.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String email = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
// Enter Into Hash Map
nameEmailMap.put(email, name);
}
// Get The Contents of Hash Map in Log
for (Map.Entry<String, String> entry : nameEmailMap.entrySet()) {
String key = entry.getKey();
Log.d(TAG, "Email :" + key);
String value = entry.getValue();
Log.d(TAG, "Name :" + value);
}
emails.close();
}
Remember in the above example the key is email and value is name so read your contents like mahaXXXX@gmail.com->Mahatma Gandhi instead of Mahatma Gandhi->mahaXXXX@gmail.com
Here is a simple way to get email id of contact from contact list. You need to pass contact id of user in below method and it will return you email id if exists
public String getEmail(String contactId) {
String emailStr = "";
final String[] projection = new String[]{ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Email.TYPE};
Cursor emailq = managedQuery(ContactsContract.CommonDataKinds.Email.CONTENT_URI, projection, ContactsContract.Data.CONTACT_ID + "=?", new String[]{contactId}, null);
if (emailq.moveToFirst()) {
final int contactEmailColumnIndex = emailq.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
while (!emailq.isAfterLast()) {
emailStr = emailStr + emailq.getString(contactEmailColumnIndex) + ";";
emailq.moveToNext();
}
}
return emailStr;
}
And also if you want to learn how to get contact list in your app follow this link: show contact list in app android - trinitytuts
ReferenceURL : https://stackoverflow.com/questions/10117049/get-only-email-address-from-contact-list-android
'Program Tip' 카테고리의 다른 글
foreach 루프에서 반복 횟수 계산 (0) | 2020.12.26 |
---|---|
Switch 케이스에서 추가 중괄호의 목적은 무엇입니까? (0) | 2020.12.26 |
jquery 데이터 테이블의 "10 개 항목 표시"선택 상자 값 변경 (0) | 2020.12.26 |
설치된 Perl 모듈의 버전을 어떻게 찾을 수 있습니까? (0) | 2020.12.26 |
줄임표가있는 문자열을 자르는 이상적인 방법 (0) | 2020.12.26 |