Posts

Showing posts with the label contacts

android - Get contacts with phone number example

activity_main.xml xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/root_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp"

Get Contact Name from Mobile Number in Android

A Simple Function to get the contact Name from Mobile Number in Android public String retrieveContactRecord(Context context, String phoneNo) { try { Uri uri = Uri.withAppendedPath( ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNo)); String[] projection = new String[] { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME }; String selection = null; String[] selectionArgs = null; String sortOrder = ContactsContract.PhoneLookup.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; ContentResolver cr = context.getContentResolver(); if (cr != null) { Cursor resultCur = cr.query(uri, projection, selection, selectionArgs, sortOrder); if (resultCur != null) { while (resultCur.moveToNext()) { contactId = resultCur .getString(resultCur .getColumnIndex(ContactsContract.PhoneLookup._ID)); contactName = resultCur .getString(resultCur .getColumnIn...