get Person Name by id and address from Contact - Android Account

Android examples for Account:Contact Name

Description

get Person Name by id and address from Contact

Demo Code

/*/*  w ww  . j  av a2 s  . co m*/
    This file is part of dgAlert Classic.

    dgAlert Classic is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    dgAlert Classic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with dgAlert Classic.  If not, see <http://www.gnu.org/licenses/>.
 */
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Contacts;
import android.provider.Contacts.PeopleColumns;
import android.telephony.PhoneNumberUtils;

public class Main {
  public static String getPersonName(Context context, String id, String address) {
    if (id == null) {
      if (address != null) {
        // Log.v("Contact not found, formatting number");
        return PhoneNumberUtils.formatNumber(address);
      } else {
        return null;
      }
    }

    Cursor cursor = context.getContentResolver().query(Uri.withAppendedPath(Contacts.People.CONTENT_URI, id),
        new String[] { PeopleColumns.DISPLAY_NAME }, null, null, null);
    if (cursor != null) {
      try {
        if (cursor.getCount() > 0) {
          cursor.moveToFirst();
          String name = cursor.getString(0);
          return name;
        }
      } finally {
        cursor.close();
      }
    }

    if (address != null) {
      return PhoneNumberUtils.formatNumber(address);
    }
    return null;
  }
}

Related Tutorials