get Name From Contacts By Number - Android Account

Android examples for Account:Contact Name

Description

get Name From Contacts By Number

Demo Code


import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Contacts.People;
import android.provider.ContactsContract;

public class Main{
    //from w ww  .ja  va2s.  co m
    public static String getNameFromContactsByNumber(Context con,
            String number) {
        String name = null;

        String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER };
        Cursor cursor = null;
        try {
            cursor = con.getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    projection, null, null, null);
            if (cursor != null && cursor.getCount() > 0) {
                cursor.moveToFirst();
                String newNumber = "";
                do {
                    newNumber = cursor
                            .getString(cursor
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    newNumber = StringUtil.getRidofSpeciall(newNumber);
                    if (newNumber.contains(number)
                            || number.contains(newNumber)) {
                        name = cursor
                                .getString(cursor
                                        .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
                        break;
                    }
                } while (cursor.moveToNext());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }

        }

        if (StringUtil.isNull(name)) {
            Cursor cur = null;
            try {
                cur = con.getContentResolver().query(
                        Uri.parse("content://icc/adn"), null, null, null,
                        null);
                if (cur != null && cur.getCount() > 0) {
                    cur.moveToFirst();
                    String num = "";
                    do {
                        num = cur.getString(cur
                                .getColumnIndex(People.NUMBER));
                        num = StringUtil.getRidofSpeciall(num);
                        if (num.contains(number) || number.contains(num)) {
                            name = cur.getString(cur
                                    .getColumnIndex(People.NAME))
                                    + ":"
                                    + num;
                            break;
                        }

                    } while (cur.moveToNext());
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (cur != null) {
                    cur.close();
                }
            }
        }

        return name;
    }
}

Related Tutorials