如何在Android中获取联系人的电话号码

时间:2022-04-30 02:02:09

My code is as below:

我的代码如下:

String[] columns = {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER};
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, columns, null, null, null);

int ColumeIndex_ID = cursor.getColumnIndex(ContactsContract.Contacts._ID);
int ColumeIndex_DISPLAY_NAME = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
int ColumeIndex_HAS_PHONE_NUMBER = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);

while(cursor.moveToNext()) 
{   
    String id = cursor.getString(ColumeIndex_ID);
    String name = cursor.getString(ColumeIndex_DISPLAY_NAME);
    String has_phone = cursor.getString(ColumeIndex_HAS_PHONE_NUMBER);

    if(!has_phone.endsWith("0")) 
    {
        System.out.println(name);
        GetPhoneNumber(id);
    }           
}

cursor.close();


public String GetPhoneNumber(String id) 
{
    String number = "";
    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone._ID + " = " + id, null, null);

    if(phones.getCount() > 0) 
    {
        while(phones.moveToNext()) 
        {
            number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        }
        System.out.println(number);
    }

    phones.close();

    return number;
}

I get contacts' name success, but get phone number fail in GetPhoneNumber().
The phones.getCount() always equal 0.
How can I modify?

我的联系人姓名成功,但GetPhoneNumber()中的电话号码失败。 phones.getCount()始终等于0.我该如何修改?

6 个解决方案

#1


61  

Android Contact API For 2.0

Android Contact API For 2.0

//
//  Find contact based on name.
//
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
    "DISPLAY_NAME = '" + NAME + "'", null, null);
if (cursor.moveToFirst()) {
    String contactId =
        cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
    //
    //  Get all phone numbers.
    //
    Cursor phones = cr.query(Phone.CONTENT_URI, null,
        Phone.CONTACT_ID + " = " + contactId, null, null);
    while (phones.moveToNext()) {
        String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
        int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
        switch (type) {
            case Phone.TYPE_HOME:
                // do something with the Home number here...
                break;
            case Phone.TYPE_MOBILE:
                // do something with the Mobile number here...
                break;
            case Phone.TYPE_WORK:
                // do something with the Work number here...
                break;
        }
    }
    phones.close();
}
cursor.close();

For more information see this link

有关更多信息,请参阅此链接

#2


7  

try this.

    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            Log.i("Names", name);
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
            {
                // Query phone here. Covered next
                Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null); 
                while (phones.moveToNext()) { 
                         String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                         Log.i("Number", phoneNumber);
                        } 
                phones.close(); 
            }

        }
    }

#3


3  

You need permission like -

你需要这样的许可 -

 android:name="android.permission.READ_CONTACTS"/>

Then, Calling the Contact Picker

然后,调用联系人选择器

 Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

Then,

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);

switch (reqCode) {
case (PICK_CONTACT) :
   if (resultCode == Activity.RESULT_OK) {
   Uri contactData = data.getData();
   Cursor c =  managedQuery(contactData, null, null, null, null);
    if (c.moveToFirst()) {
     String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
     // TODO Whatever you want to do with the selected contact name.
   }
 }
 break;
 }
}

#4


2  

Old question but I don't see the following answer here.

老问题,但我在这里没有看到以下答案。

private static final String[] PROJECTION ={
        ContactsContract.Contacts._ID,
        ContactsContract.CommonDataKinds.Phone.NUMBER,
        ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
        ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
        ContactsContract.Contacts.LOOKUP_KEY,
};

new CursorLoader(
        this,
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
        PROJECTION,
        null,
        null,//mSelectionArgs,
        ContactsContract.Contacts.DISPLAY_NAME_PRIMARY
);

Use column index ContactsContract.CommonDataKinds.Phone.NUMBER to retreive the phone number from the cursor.

使用列索引ContactsContract.CommonDataKinds.Phone.NUMBER从光标中检索电话号码。

#5


0  

U can use contact picker.

你可以使用联系人选择器。

Call contact picker on any button and use the below code :

在任何按钮上拨打联系人选择器并使用以下代码:

|*| Add in : AndroidManifest.xml

| * |添加:AndroidManifest.xml

<uses-permission android:name="android.permission.READ_CONTACTS"/>

|*| Add in : activity_name.java

| * |添加:activity_name.java

void calContctPickerFnc()
{
    Intent calContctPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    calContctPickerIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
    startActivityForResult(calContctPickerIntent, 1);
}

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data)
{
    super.onActivityResult(reqCode, resultCode, data);

    switch (reqCode)
    {
        case (1) :
            if (resultCode == Activity.RESULT_OK)
            {
                Uri contctDataVar = data.getData();

                Cursor contctCursorVar = getContentResolver().query(contctDataVar, null,
                        null, null, null);
                if (contctCursorVar.getCount() > 0)
                {
                    while (contctCursorVar.moveToNext())
                    {
                        String ContctUidVar = contctCursorVar.getString(contctCursorVar.getColumnIndex(ContactsContract.Contacts._ID));

                        String ContctNamVar = contctCursorVar.getString(contctCursorVar.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                        Log.i("Names", ContctNamVar);

                        if (Integer.parseInt(contctCursorVar.getString(contctCursorVar.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
                        {
                            // Query phone here. Covered next
                            String ContctMobVar = contctCursorVar.getString(contctCursorVar.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            Log.i("Number", ContctMobVar);
                        }

                    }
                }
            }
            break;
    }
}

#6


0  

Your can add this code on your Activity class:

您可以在Activity类上添加此代码:

    private final int PICK_CONTACT = 5;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);

    Intent intent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.Contacts.CONTENT_URI);
            intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
            startActivityForResult(intent, PICK_CONTACT);

    //Your code
   }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == PICK_CONTACT && resultCode == RESULT_OK) {
        Uri contactData = data.getData();

        Cursor phones = getContentResolver()
                .query(contactData,
                        null,
                        null,
                        null,
                        null);

        String name = "", phoneNumber = "";

        while (phones.moveToNext()) {
            name = phones.getString(phones.getColumnIndex(
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            phoneNumber = phones.getString(phones.getColumnIndex(
                    ContactsContract.CommonDataKinds.Phone.NUMBER));

        }
        phones.close();
    }
  }

#1


61  

Android Contact API For 2.0

Android Contact API For 2.0

//
//  Find contact based on name.
//
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
    "DISPLAY_NAME = '" + NAME + "'", null, null);
if (cursor.moveToFirst()) {
    String contactId =
        cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
    //
    //  Get all phone numbers.
    //
    Cursor phones = cr.query(Phone.CONTENT_URI, null,
        Phone.CONTACT_ID + " = " + contactId, null, null);
    while (phones.moveToNext()) {
        String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
        int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
        switch (type) {
            case Phone.TYPE_HOME:
                // do something with the Home number here...
                break;
            case Phone.TYPE_MOBILE:
                // do something with the Mobile number here...
                break;
            case Phone.TYPE_WORK:
                // do something with the Work number here...
                break;
        }
    }
    phones.close();
}
cursor.close();

For more information see this link

有关更多信息,请参阅此链接

#2


7  

try this.

    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            Log.i("Names", name);
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
            {
                // Query phone here. Covered next
                Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null); 
                while (phones.moveToNext()) { 
                         String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                         Log.i("Number", phoneNumber);
                        } 
                phones.close(); 
            }

        }
    }

#3


3  

You need permission like -

你需要这样的许可 -

 android:name="android.permission.READ_CONTACTS"/>

Then, Calling the Contact Picker

然后,调用联系人选择器

 Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

Then,

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);

switch (reqCode) {
case (PICK_CONTACT) :
   if (resultCode == Activity.RESULT_OK) {
   Uri contactData = data.getData();
   Cursor c =  managedQuery(contactData, null, null, null, null);
    if (c.moveToFirst()) {
     String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
     // TODO Whatever you want to do with the selected contact name.
   }
 }
 break;
 }
}

#4


2  

Old question but I don't see the following answer here.

老问题,但我在这里没有看到以下答案。

private static final String[] PROJECTION ={
        ContactsContract.Contacts._ID,
        ContactsContract.CommonDataKinds.Phone.NUMBER,
        ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
        ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
        ContactsContract.Contacts.LOOKUP_KEY,
};

new CursorLoader(
        this,
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
        PROJECTION,
        null,
        null,//mSelectionArgs,
        ContactsContract.Contacts.DISPLAY_NAME_PRIMARY
);

Use column index ContactsContract.CommonDataKinds.Phone.NUMBER to retreive the phone number from the cursor.

使用列索引ContactsContract.CommonDataKinds.Phone.NUMBER从光标中检索电话号码。

#5


0  

U can use contact picker.

你可以使用联系人选择器。

Call contact picker on any button and use the below code :

在任何按钮上拨打联系人选择器并使用以下代码:

|*| Add in : AndroidManifest.xml

| * |添加:AndroidManifest.xml

<uses-permission android:name="android.permission.READ_CONTACTS"/>

|*| Add in : activity_name.java

| * |添加:activity_name.java

void calContctPickerFnc()
{
    Intent calContctPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    calContctPickerIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
    startActivityForResult(calContctPickerIntent, 1);
}

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data)
{
    super.onActivityResult(reqCode, resultCode, data);

    switch (reqCode)
    {
        case (1) :
            if (resultCode == Activity.RESULT_OK)
            {
                Uri contctDataVar = data.getData();

                Cursor contctCursorVar = getContentResolver().query(contctDataVar, null,
                        null, null, null);
                if (contctCursorVar.getCount() > 0)
                {
                    while (contctCursorVar.moveToNext())
                    {
                        String ContctUidVar = contctCursorVar.getString(contctCursorVar.getColumnIndex(ContactsContract.Contacts._ID));

                        String ContctNamVar = contctCursorVar.getString(contctCursorVar.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                        Log.i("Names", ContctNamVar);

                        if (Integer.parseInt(contctCursorVar.getString(contctCursorVar.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
                        {
                            // Query phone here. Covered next
                            String ContctMobVar = contctCursorVar.getString(contctCursorVar.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            Log.i("Number", ContctMobVar);
                        }

                    }
                }
            }
            break;
    }
}

#6


0  

Your can add this code on your Activity class:

您可以在Activity类上添加此代码:

    private final int PICK_CONTACT = 5;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);

    Intent intent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.Contacts.CONTENT_URI);
            intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
            startActivityForResult(intent, PICK_CONTACT);

    //Your code
   }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == PICK_CONTACT && resultCode == RESULT_OK) {
        Uri contactData = data.getData();

        Cursor phones = getContentResolver()
                .query(contactData,
                        null,
                        null,
                        null,
                        null);

        String name = "", phoneNumber = "";

        while (phones.moveToNext()) {
            name = phones.getString(phones.getColumnIndex(
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            phoneNumber = phones.getString(phones.getColumnIndex(
                    ContactsContract.CommonDataKinds.Phone.NUMBER));

        }
        phones.close();
    }
  }