'2017/06/28'에 해당되는 글 1건

728x90

연락처 수정 기능 테스트를 하다보니 rawId를 구해야 하는 경우가 있어서 코드를 추가했다.

ContactsContract.RawContacts._ID = ContactsContract.Data.RAW_CONTACT_ID


public static long getrawIdFromContactId(ContentResolver contactHelper, long contactId) {
    // ContactsContract.RawContacts._ID = ContactsContract.Data.RAW_CONTACT_ID
    long rawId = -1;
    Uri contactUri = ContactsContract.RawContacts.CONTENT_URI;
    String[] projection = {RawContacts._ID};
    String where = RawContacts.CONTACT_ID + "=?";
    String[] whereParams = new String[]{String.valueOf(contactId)};
    Cursor cursor = null;

    try {
        cursor = contactHelper.query(contactUri, projection, where, whereParams, null);
        if (cursor.moveToFirst()) {
            rawId = cursor.getLong(cursor.getColumnIndex(RawContacts._ID));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if(cursor != null){
            cursor.close();
        }
    }
    return rawId;
}

 public static long getContactIDFromrawId(ContentResolver contactHelper, long rawId) {
    long rawContactId = -1;
    Uri contactUri = ContactsContract.RawContacts.CONTENT_URI;
    String[] projection = {RawContacts.CONTACT_ID};
    String where = RawContacts._ID + "=?";
    String[] whereParams = new String[]{String.valueOf(rawId)};
    Cursor cursor = null;

    try {
        cursor = contactHelper.query(contactUri, projection, where, whereParams, null);
        if (cursor.moveToFirst()) {
            rawContactId = cursor.getLong(cursor.getColumnIndex(RawContacts.CONTACT_ID));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if(cursor != null){
            cursor.close();
        }
    }
    return rawContactId;
}


블로그 이미지

Link2Me

,