안드로이드 개발 질문/답변
(글 수 45,052)
안녕하세요.
contacts에서 email주소를 가져오는 방법을 찾고 있습니다.
풀 소스랑 검색을 해봤지만 잘 안나오네여...
조금 급해서 이렇게 부탁드립니다. 감사합니다.
2009.12.28 15:25:37
소스상에서 Contacts DB를 가져오는 부분입니다.
일단 email을 가져오는 부분은 도움을 받아 해결했습니다.
int idx = Utils.getContactsIDbyPhonenumber(_phonenumber);
if(idx == -1) return null;
Uri mUri = ContentUris.withAppendedId(People.CONTENT_URI, idx );
Uri uri = Uri.withAppendedPath(mUri, Contacts.People.ContactMethods.CONTENT_DIRECTORY);
String selection = Contacts.ContactMethodsColumns.KIND + "=?";
String value = String.valueOf(Contacts.KIND_EMAIL);
String[] args = new String[] {value};
ContentResolver resolver = mMain.getContentResolver();
Cursor cursor = resolver.query(uri, null, selection, args, null);
int rc = cursor.getCount();
if(cursor.moveToFirst()) {
// 컬럼명으로 컬럼 인덱스 찾기
int iId = cursor.getColumnIndex(Contacts.ContactMethods._ID);
int iType = cursor.getColumnIndex(Contacts.ContactMethods.TYPE);
int iData = cursor.getColumnIndex(Contacts.ContactMethods.DATA);
// 요소값 얻기
int id = cursor.getInt(iId);
int type = cursor.getInt(iType);
String email = cursor.getString(iData);
return email;
}
너무 급해서 두서없이 질문을 올렸습니다. 지송 ^^
일단 email을 가져오는 부분은 도움을 받아 해결했습니다.
int idx = Utils.getContactsIDbyPhonenumber(_phonenumber);
if(idx == -1) return null;
Uri mUri = ContentUris.withAppendedId(People.CONTENT_URI, idx );
Uri uri = Uri.withAppendedPath(mUri, Contacts.People.ContactMethods.CONTENT_DIRECTORY);
String selection = Contacts.ContactMethodsColumns.KIND + "=?";
String value = String.valueOf(Contacts.KIND_EMAIL);
String[] args = new String[] {value};
ContentResolver resolver = mMain.getContentResolver();
Cursor cursor = resolver.query(uri, null, selection, args, null);
int rc = cursor.getCount();
if(cursor.moveToFirst()) {
// 컬럼명으로 컬럼 인덱스 찾기
int iId = cursor.getColumnIndex(Contacts.ContactMethods._ID);
int iType = cursor.getColumnIndex(Contacts.ContactMethods.TYPE);
int iData = cursor.getColumnIndex(Contacts.ContactMethods.DATA);
// 요소값 얻기
int id = cursor.getInt(iId);
int type = cursor.getInt(iType);
String email = cursor.getString(iData);
return email;
}
너무 급해서 두서없이 질문을 올렸습니다. 지송 ^^
2009.12.29 11:21:11
String[] projection = new String[] {
Contacts.Phones.DISPLAY_NAME,
Contacts.Phones.NUMBER };
Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(number));
// query time
Cursor c = getContentResolver().query(contactUri, projection, null,
null, null);
// 위에서 조건을 주면 되겠죠?
String name = c.getString(c
.getColumnIndex(Contacts.Phones.DISPLAY_NAME))
WHERE가 없으면 결과가 많겠죠..? 그럼 루프를 돌아야 할테고
처음 하시는분 참고 하시라 달아 봅니다.