Android Phone Number Get getPhoneNumber(int contactId)

Here you can find the source of getPhoneNumber(int contactId)

Description

This method used to get phone number from contact id.

Parameter

Parameter Description
contactId represented contact id

Return

represented

Declaration

@SuppressWarnings("deprecation")
private static String getPhoneNumber(int contactId) 

Method Source Code

//package com.java2s;

import android.app.Activity;

import android.database.Cursor;

import android.provider.ContactsContract.CommonDataKinds.Phone;

import android.provider.ContactsContract.Data;

public class Main {
    public static Activity mSmartAndroidActivity;

    /**//  www  . j a v  a2 s.  c o m
     * This method used to get phone number from contact id.
     * 
     * @param contactId
     *            represented contact id
     * @return represented {@link String}
     */
    @SuppressWarnings("deprecation")
    private static String getPhoneNumber(int contactId) {

        String phoneNumber = "";
        final String[] projection = new String[] { Phone.NUMBER,
                Phone.TYPE, };
        final Cursor phone = mSmartAndroidActivity.managedQuery(
                Phone.CONTENT_URI, projection, Data.CONTACT_ID + "=?",
                new String[] { String.valueOf(contactId) }, null);

        if (phone.moveToFirst()) {
            final int contactNumberColumnIndex = phone
                    .getColumnIndex(Phone.DATA);

            while (!phone.isAfterLast()) {
                phoneNumber = phoneNumber
                        + phone.getString(contactNumberColumnIndex) + ";";
                phone.moveToNext();
            }

        }
        phone.close();
        return phoneNumber;
    }
}

Related

  1. getPhoneNumber(Context context)
  2. getPlainPhoneNumber(String phoneNumber)
  3. getMy10DigitPhoneNumber(Context ctx)
  4. getMyPhoneNumber(Context ctx)