Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.Context;

import android.database.Cursor;

import android.net.Uri;
import android.os.Build;
import android.provider.ContactsContract;

import android.util.Log;

public class Main {
    public static String getContactName(Context context, String number) {

        String name = null;

        // define the columns I want the query to return
        String[] projection = new String[] { Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB
                ? ContactsContract.Contacts.DISPLAY_NAME_PRIMARY
                : ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.PhoneLookup._ID };

        // encode the phone number and build the filter URI
        Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

        // query time
        Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);

        if (cursor != null) {
            if (cursor.moveToFirst()) {
                name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
                Log.v("getContactName", "Started uploadcontactphoto: Contact Found @ " + number);
                Log.v("getContactName", "Started uploadcontactphoto: Contact name  = " + name);
            } else {
                Log.v("getContactName", "Contact Not Found @ " + number);
            }
            cursor.close();
        }
        return name;
    }
}