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 {
    /**
     * Get the contact name from a URI.
     *
     * @param context The context.
     * @param contactUri The contact URI.
     *
     * @return The contact name.
     */
    public static String getContactName(final Context context, Uri contactUri) {
        String name = null;
        if (contactUri != null) {
            Cursor cursor = null;
            try {
                cursor = context.getContentResolver().query(contactUri,
                        new String[] { ContactsContract.Contacts.DISPLAY_NAME }, null, null, null);
                if (cursor != null && cursor.moveToFirst()) {
                    name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                }
            } finally {
                if (cursor != null) {
                    cursor.close();
                }

            }
        }

        return name;
    }
}