Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;

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

import android.provider.ContactsContract.PhoneLookup;

public class Main {
    public static int getContactIdFromPhoneNumber(final Context context, final String number) {
        final Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
        final String[] projection = { PhoneLookup._ID };
        final Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
        if (c.getCount() > 0) {
            c.moveToFirst();
            return c.getInt(0);
        } else {
            return -1;
        }
    }

    private static int getInt(final Cursor cursor, final String field, final int defValue) {
        if (cursor.getColumnIndex(field) >= 0) {
            return cursor.getInt(cursor.getColumnIndex(field));
        } else {
            return defValue;
        }
    }
}