get Person Id From Phone Number - Android Account

Android examples for Account:Contact ID

Description

get Person Id From Phone Number

Demo Code

/*//from   w ww .  j  av  a 2s. c  o  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;

public class Main {
  public static String getPersonIdFromPhoneNumber(Context context, String address) {
    if (address == null)
      return null;

    Cursor cursor = context.getContentResolver().query(
        Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, address), new String[] { Contacts.Phones.PERSON_ID },
        null, null, null);
    if (cursor != null) {
      try {
        if (cursor.getCount() > 0) {
          cursor.moveToFirst();
          Long id = Long.valueOf(cursor.getLong(0));

          return (String.valueOf(id));
        }
      } finally {
        cursor.close();
      }
    }
    return null;
  }
}

Related Tutorials