Android从联系人列表中获取电话号码

时间:2021-01-31 22:55:15

Here ,code to get phone number to retrive phone number from contactlist . it works fro "name" . but i can not get phonenumber please give me code to get phone number.Thanks in Advance........

在这里,代码获取电话号码以从联系人列表中检索电话号码。它适用于“名称”。但我不能得到phonenumber请给我代码来获取电话号码。谢谢你提前........

    cr = getContentResolver(); 
    contactList = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null,   null,null);      
    if(contactList.getCount() > 0)
    {while(contactList.moveToNext())
    {id = contactList.getString(contactList.             getColumnIndex(ContactsContract.Contacts._ID));
    name = contactList.getString(contactList.         getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    number = contactList.getString(contactList.
getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        cntctArrayList.add(number);             
    Log.e("", name);
    System.out.println("Contact_id:"+id+"Contact name:"+number);}
}

2 个解决方案

#1


8  

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));
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 phoneNo =   pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    }

                }
            }
  }

#2


0  

public void getContactPhoneNumbers()
{       
    Cursor c1 = this.getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

    String personName = null, number = null;
    try 
    {
        if(c1.getCount() > 0)
        {
            int i=0;
            while(c1.moveToNext() && i<10)
            {
                i++;
                String id = c1.getString(
                        c1.getColumnIndex(Contacts._ID));
                //Below code will get contact name
                personName = c1.getString(
                        c1.getColumnIndex(Contacts.DISPLAY_NAME));
                Toast.makeText(getApplicationContext(), 
                        "name.."+personName,
                        0).show();
                //based on id, now you can find email of that person
                Cursor cn = this.getContentResolver().
                            query(CommonDataKinds.Email.CONTENT_URI, 
                            null, 
                            CommonDataKinds.Email.CONTACT_ID +" = ?", 
                            new String[]{id}, null);
                while(cn.moveToNext())
                {
                    number = cn.getString(
                            cn.getColumnIndex(
                                    CommonDataKinds.Email.ADDRESS));
                    int type = cn.getInt(
                            cn.getColumnIndex(
                                    CommonDataKinds.Phone.TYPE));
                    Toast.makeText(getApplicationContext(), 
                            "email.."+number+".."+type,
                            0).show();
                }
                //based on id, you can also now find his phone numbers
                Cursor cur = this.getContentResolver().
                            query(CommonDataKinds.Phone.CONTENT_URI, 
                            null, 
                            CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                            new String[]{id}, null);
                while(cur.moveToNext())
                {
                    number = cur.getString(
                            cur.getColumnIndex(
                                    CommonDataKinds.Phone.NUMBER));
                    int type = cur.getInt(
                            cur.getColumnIndex(
                                    CommonDataKinds.Phone.TYPE));
                    Toast.makeText(getApplicationContext(), 
                            "number.."+number+".."+type,
                            0).show();
                }
            }
        }
    }
    finally
    {
        c1.close();
    }
}

#1


8  

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));
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 phoneNo =   pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    }

                }
            }
  }

#2


0  

public void getContactPhoneNumbers()
{       
    Cursor c1 = this.getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

    String personName = null, number = null;
    try 
    {
        if(c1.getCount() > 0)
        {
            int i=0;
            while(c1.moveToNext() && i<10)
            {
                i++;
                String id = c1.getString(
                        c1.getColumnIndex(Contacts._ID));
                //Below code will get contact name
                personName = c1.getString(
                        c1.getColumnIndex(Contacts.DISPLAY_NAME));
                Toast.makeText(getApplicationContext(), 
                        "name.."+personName,
                        0).show();
                //based on id, now you can find email of that person
                Cursor cn = this.getContentResolver().
                            query(CommonDataKinds.Email.CONTENT_URI, 
                            null, 
                            CommonDataKinds.Email.CONTACT_ID +" = ?", 
                            new String[]{id}, null);
                while(cn.moveToNext())
                {
                    number = cn.getString(
                            cn.getColumnIndex(
                                    CommonDataKinds.Email.ADDRESS));
                    int type = cn.getInt(
                            cn.getColumnIndex(
                                    CommonDataKinds.Phone.TYPE));
                    Toast.makeText(getApplicationContext(), 
                            "email.."+number+".."+type,
                            0).show();
                }
                //based on id, you can also now find his phone numbers
                Cursor cur = this.getContentResolver().
                            query(CommonDataKinds.Phone.CONTENT_URI, 
                            null, 
                            CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                            new String[]{id}, null);
                while(cur.moveToNext())
                {
                    number = cur.getString(
                            cur.getColumnIndex(
                                    CommonDataKinds.Phone.NUMBER));
                    int type = cur.getInt(
                            cur.getColumnIndex(
                                    CommonDataKinds.Phone.TYPE));
                    Toast.makeText(getApplicationContext(), 
                            "number.."+number+".."+type,
                            0).show();
                }
            }
        }
    }
    finally
    {
        c1.close();
    }
}