如何使用Android读取联系人号码如何使用、联系人、号码、Android

2023-09-06 23:26:08 作者:小楫夜泊

我想读采用Android的电话号码和姓名。我用下面的code。它只能读取手机名称。我怎么能读取手机号码。我对Android的非常新。可能任何一个HLEP PLZ。

I want to read the phone number and name using Android. I used the following code. It can only read phone name. How can I read phone number. I am very new in Android. Could any one hlep plz.

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));               
       if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
    {
          Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);    
        while (pCur.moveToNext()) 
            {
               String lnumber = cur.getString( cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            } 
        pCur.close();
    }                  
    }

先谢谢了。

推荐答案

使用以下code一种简单的方法来阅读显示的姓名和电话号码...!

Use the following code for an easy way to Read display name and phone number...!

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
  String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
  String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();

谢谢....!