Get contacts Numbers to String array - Android Account

Android examples for Account:Contact Number

Description

Get contacts Numbers to String array

Demo Code


//package com.java2s;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static String[] contactsNumbers(Context ctx) {

        List<String> phoneList = new ArrayList<String>();

        Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
        String _ID = ContactsContract.Contacts._ID;
        String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
        Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
        String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;

        ContentResolver contentResolver = ctx.getContentResolver();
        Cursor cursor = contentResolver.query(CONTENT_URI, null, null,
                null, null);//from w  w  w. j  a  v  a  2s . com

        // Loop for every contact in the phone
        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {

                String contact_id = cursor.getString(cursor
                        .getColumnIndex(_ID));
                int hasPhoneNumber = Integer
                        .parseInt(cursor.getString(cursor
                                .getColumnIndex(HAS_PHONE_NUMBER)));
                if (hasPhoneNumber > 0) {

                    // Query and loop for every phone number of the contact
                    Cursor phoneCursor = contentResolver.query(
                            PhoneCONTENT_URI, null, Phone_CONTACT_ID
                                    + " = ?", new String[] { contact_id },
                            null);
                    while (phoneCursor.moveToNext()) {
                        phoneList.add(phoneCursor.getString(
                                phoneCursor.getColumnIndex(NUMBER))
                                .replaceAll("[\\D]", ""));
                    }
                    phoneCursor.close();
                }
            }
        }
        return phoneList.toArray(new String[phoneList.size()]);
    }
}

Related Tutorials