get Contact ID - Android Account

Android examples for Account:Contact ID

Description

get Contact ID

Demo Code


//package com.java2s;

import android.content.ContentResolver;

import android.database.Cursor;
import android.net.Uri;

import android.provider.ContactsContract.PhoneLookup;

public class Main {
    private static long getContactID(ContentResolver contactHelper,
            String number) {/*from   ww  w . ja  va 2  s.  c  om*/
        Uri contactUri = Uri.withAppendedPath(
                PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

        String[] projection = { PhoneLookup._ID };
        Cursor cursor = null;

        try {
            cursor = contactHelper.query(contactUri, projection, null,
                    null, null);

            if (cursor.moveToFirst()) {
                int personID = cursor.getColumnIndex(PhoneLookup._ID);
                return cursor.getLong(personID);
            }

            return -1;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
                cursor = null;
            }
        }

        return -1;
    }
}

Related Tutorials