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;

public class Main {
    /**
     * Gets a contact number and then displays the name of contact in the DP
     *
     * @param context       context of the activity from which it was called.
     * @param contactNumber a string representation of the contact number
     * @return returns the number if no contact exist.
     */
    public static String getContactName(Context context, String contactNumber) {
        String displayName = null;
        Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(contactNumber));

        Cursor cur = context.getContentResolver().query(uri,
                new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);

        if (cur != null && cur.moveToFirst()) {
            displayName = cur.getString(cur.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
        } else {
            displayName = contactNumber;
        }

        if (!cur.isClosed()) {
            cur.close();
        }

        return displayName;
    }
}