Android examples for Phone:Phone Number
return Contact Name by phone number
/*/*from w w w. j a va 2s . com*/ * Copyright (C) 2013 Android Open Kang Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.ContactsContract.PhoneLookup; public class Main { public static String returnContactName(Context context, String phoneNumber) { String contactName = null; Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); String[] numberProject = { PhoneLookup.DISPLAY_NAME }; Cursor c = context.getContentResolver().query(lookupUri, numberProject, null, null, null); try { if (c.moveToFirst()) { contactName = c.getString(c.getColumnIndex(PhoneLookup.DISPLAY_NAME)); } else { // Not in contacts, return number again contactName = phoneNumber; } } finally { if (c != null) { c.close(); } } return contactName; } }