Android 以编程方式从联系人获取设备默认 phone 号码

Android programatically getting device default phone number from contacts

我使用此方法

从android"my"联系人那里获得了姓名和联系人ID
                 String[] columnNames = new String[] { Phone._ID,
                        Phone.DISPLAY_NAME };

                Cursor c = LoginActivity.this.getContentResolver().query(
                        ContactsContract.Profile.CONTENT_URI, columnNames,
                        null, null, null);
                int count = c.getCount();
                boolean b = c.moveToFirst();
                int position = c.getPosition();
                if (count == 1 && position == 0) {
                    for (int j = 0; j < columnNames.length; j++) {
                        contact_id = c.getString(0);
                        namee = c.getString(1);


                    }
                }

如何获取设备的phone号?

试试这个:

Cursor cursor = null;
    try {
        cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
        int contactIdIdx = cursor.getColumnIndex(Phone._ID);
        int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
        int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
        int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_ID);
        cursor.moveToFirst();
        do {
            String idContact = cursor.getString(contactIdIdx);
            String name = cursor.getString(nameIdx);
            String phoneNumber = cursor.getString(phoneNumberIdx);

        } while (cursor.moveToNext());  
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

清单中:

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

试试这个...

String[] columnNames = new String[] { Phone._ID,
                            Phone.DISPLAY_NAME };

        Cursor c = LoginActivity.this.getContentResolver().query(
                            ContactsContract.Profile.CONTENT_URI, columnNames,
                            null, null, null);
         int count = c.getCount();
         boolean b = c.moveToFirst();
         int position = c.getPosition();
         if (count == 1 && position == 0) {
         for (int j = 0; j < columnNames.length; j++) {
             contact_id = c.getString(0);
             namee = c.getString(1); 
      if(Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

                  Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                    null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?",new String[] { contact_id  }, null);


              while (pCur.moveToNext()) {
                  String noo = pCur.getString(pCur.getColumnIndex(CommonDataKinds.Phone.NUMBER));
                }
          }
     }
                    } 

这是正确的做法

1) 调用意图

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
if(intent.resolveActivity(getActivity().getPackageManager()) != null) {
    getParentFragment().startActivityForResult(intent, CONTACT_REQUEST_CODE);
} 

2) 实现 onActivityResult

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

        if (resultCode == Activity.RESULT_OK) {

            if (requestCode == CONTACT_REQUEST_CODE) {

                uriContact = data.getData();

                // replaceAll("\D", "") : Remove everything that is not a digit
                String number = retrieveContactNumber();
                if (Connectivity.isConnected(getActivity())) {
                    if (number != null) {
                        String contact_number = number.replaceAll("\D", "");
                    } 
                }
            }
      }
}

3) 实施 retrieveContactNumber()

private String retrieveContactNumber() {

        String contactNumber = null;

        // getting contacts ID
        Cursor cursorID = getActivity().getContentResolver().query(uriContact,
                new String[]{ContactsContract.Contacts._ID},
                null, null, null);

        if (cursorID.moveToFirst()) {
            contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
        }

        cursorID.close();

        // Using the contact ID to get contact phone number
        Cursor cursorPhone = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},

                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
                        ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
                        ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,

                new String[]{contactID},
                null);

        if (cursorPhone.moveToFirst()) {
            contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        }

        cursorPhone.close();

        return contactNumber;
    }

4) 享受 :)

我认为你应该使用 ContactsContract.CommonDataKinds.Phone.CONTENT_URI 并且有一个名为 ContactsContract.CommonDataKinds.Phone.NUMBER 的列,你可以使用它获得联系电话。

看看这段代码

public ArrayList<String> getContactList(){

         ArrayList<String> list= new ArrayList<String>();

            Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            String[] columns    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                            ContactsContract.CommonDataKinds.Phone.NUMBER};

            Cursor people = getContentResolver().query(uri, columns, null, null, null);

            int _nameIndex = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            int _numberIndex = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

            people.moveToFirst();
            do {
                String name   = people.getString(_nameIndex);
                String number = people.getString(_numberIndex);
                list.add(name+" :"+number);
                // Do work...
            } while (people.moveToNext());

        return list;
}

这对我有用。

            String[] columnNames = new String[] { Phone._ID,
                    Phone.DISPLAY_NAME, Phone.NUMBER };

            Cursor c = LoginActivity.this.getContentResolver().query(
                    ContactsContract.Profile.CONTENT_URI, columnNames,
                    null, null, null);
            int count = c.getCount();
            boolean b = c.moveToFirst();
            int position = c.getPosition();
            if (count == 1 && position == 0) {
                for (int j = 0; j < columnNames.length; j++) {
                    contact_id = c.getString(0);
                    name = c.getString(1);
                    number= c.getString(2);
                }
            }

在清单中添加以下权限。

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

使用以下代码获取 "Me" 联系人。

    public Loader<Cursor> onCreateLoader(int id, Bundle arguments) {
    return new CursorLoader(this,
            // Retrieve data rows for the device user's 'profile' contact.
            Uri.withAppendedPath( ContactsContract.Profile.CONTENT_URI,ContactsContract.Contacts.Data.CONTENT_DIRECTORY),
            ProfileQuery.PROJECTION,

            //Don't select anything here null will return all available fields

            null,
            null,                   
            null);
}
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
    ArrayList<String> DataArray = new ArrayList<String>();
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
                //here where you get your data and its type
        TypeName=cursor.getString(ProfileQuery.ADDRESS);//this will give you field name
        Data=cursor.getString(ProfileQuery.NUMBER);//this will give you field data

        cursor.moveToNext();
    }
}
@Override
public void onLoaderReset(Loader<Cursor> cursorLoader) {
}
private interface ProfileQuery {
    String[] PROJECTION = {
            ContactsContract.Contacts.Data.MIMETYPE,
            ContactsContract.CommonDataKinds.Email.ADDRESS  ,
            ContactsContract.CommonDataKinds.Email.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Organization.DATA3,

    };

    int ADDRESS = 0;
    int NUMBER = 1;
}