Get the device owner's contact on Android

2

In the Contacts application, the first one shown is the owner's.

Is it possible to extract contact information from the owner of the device?

I have searched for all the contacts available from the provider ContactsContract.Contacts and I do not see in any case that indexes it.

I wonder, if it is stored in another corner or is it only internal use of the system contacts app?

Edit

Busancdo by the SDK I found how to get the data of the owner of the device

public static SimpleContact TESTgetLocalProfile(Context context) {

    Uri PROFILE_URI = ContactsContract.Profile.CONTENT_URI;
    String[] PROJECTION = new String[]{
            ContactsContract.Profile._ID,
            ContactsContract.Profile.DISPLAY_NAME,
            ContactsContract.Profile.PHOTO_URI,
            ContactsContract.Profile.HAS_PHONE_NUMBER,
            ContactsContract.Profile.LOOKUP_KEY
    };
    final String SELECTION = ContactsContract.CommonDataKinds.Email.CONTACT_ID + "= ?";
    String[] mSelectionArgs = {"10"};
    String sortOrder = ContactsContract.CommonDataKinds.Email.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    // Build adapter with contact entries
    Cursor c = context.getContentResolver().query(PROFILE_URI, PROJECTION, null, null, null);

    SimpleContact item = null;

    if (c != null && c.moveToFirst()) {
        String contactId = c.getString(c.getColumnIndex(ContactsContract.Profile._ID));
        item = new SimpleContact(contactId);
        item.setDisplayName(c.getString(c.getColumnIndex(ContactsContract.Profile.DISPLAY_NAME)));
        item.setPhoto(c.getString(c.getColumnIndex(ContactsContract.Profile.PHOTO_URI)));
        item.setLookupKey(c.getString(c.getColumnIndex(ContactsContract.Profile.LOOKUP_KEY)));
        if (c.getInt(c.getColumnIndex(ContactsContract.Profile.HAS_PHONE_NUMBER)) > 0) {
            //No ser como obtener el número
        }
        c.close();
    }

    return item;

}

Field Debug:

D/ContactsHelper: columName: sort_key =Test User
D/ContactsHelper: columName: blocked =0
D/ContactsHelper: columName: photo_uri =content://com.android.contacts/display_photo/9223372034707292161
D/ContactsHelper: columName: status_label =null
D/ContactsHelper: columName: status_ts =null
D/ContactsHelper: columName: status_res_package =null
D/ContactsHelper: columName: display_name =Test User
D/ContactsHelper: columName: last_time_used =null
D/ContactsHelper: columName: mimetype =vnd.android.cursor.item/name
D/ContactsHelper: columName: phonebook_label_alt =U
D/ContactsHelper: columName: data6 =null
D/ContactsHelper: columName: version =7
D/ContactsHelper: columName: photo_id =9223372034707292166
D/ContactsHelper: columName: data3 =User
D/ContactsHelper: columName: custom_ringtone =null
D/ContactsHelper: columName: times_contacted =0
D/ContactsHelper: columName: account_type_and_data_set =com.android.localphone
D/ContactsHelper: columName: dirty =1
D/ContactsHelper: columName: data7 =null
D/ContactsHelper: columName: data15 =null
D/ContactsHelper: columName: raw_contact_is_user_profile =1
D/ContactsHelper: columName: data_set =null
D/ContactsHelper: columName: phonebook_label =T
D/ContactsHelper: columName: data10 =1
D/ContactsHelper: columName: res_package =null
D/ContactsHelper: columName: account_type =com.android.localphone
D/ContactsHelper: columName: data11 =0
D/ContactsHelper: columName: display_name_alt =User, Test
D/ContactsHelper: columName: lookup =profile
D/ContactsHelper: columName: phonetic_name =null
D/ContactsHelper: columName: last_time_contacted =0
D/ContactsHelper: columName: contact_last_updated_timestamp =1505762023483
D/ContactsHelper: columName: data13 =null
D/ContactsHelper: columName: in_visible_group =0
D/ContactsHelper: columName: chat_capability =null
D/ContactsHelper: columName: data9 =null
D/ContactsHelper: columName: carrier_presence =0
D/ContactsHelper: columName: data_sync1 =null
D/ContactsHelper: columName: sort_key_alt =User, Test
D/ContactsHelper: columName: contact_presence =null
D/ContactsHelper: columName: data_version =0
D/ContactsHelper: columName: phonetic_name_style =0
D/ContactsHelper: columName: name_raw_contact_id =9223372034707292163
D/ContactsHelper: columName: raw_contact_id =9223372034707292163
D/ContactsHelper: columName: send_to_voicemail =0
D/ContactsHelper: columName: data4 =null
D/ContactsHelper: columName: data12 =null
D/ContactsHelper: columName: contact_status =null
D/ContactsHelper: columName: contact_status_label =null
D/ContactsHelper: columName: pinned =0
D/ContactsHelper: columName: status_icon =null
D/ContactsHelper: columName: status =null
D/ContactsHelper: columName: data1 =Test User
D/ContactsHelper: columName: phonebook_bucket =21
D/ContactsHelper: columName: data_sync2 =null
D/ContactsHelper: columName: contact_status_res_package =null
D/ContactsHelper: columName: in_default_directory =1
D/ContactsHelper: columName: _id =9223372034707292163
D/ContactsHelper: columName: hash_id =null
D/ContactsHelper: columName: is_super_primary =0
D/ContactsHelper: columName: data5 =null
D/ContactsHelper: columName: contact_id =9223372034707292162
D/ContactsHelper: columName: data8 =null
D/ContactsHelper: columName: is_primary =0
D/ContactsHelper: columName: data_sync4 =null
D/ContactsHelper: columName: has_phone_number =1
D/ContactsHelper: columName: display_name_source =40
D/ContactsHelper: columName: photo_file_id =9223372034707292161
D/ContactsHelper: columName: custom_vibration =0
D/ContactsHelper: columName: data_sync3 =null
D/ContactsHelper: columName: backup_id =null
D/ContactsHelper: columName: data14 =null
D/ContactsHelper: columName: contact_status_ts =null
D/ContactsHelper: columName: phonebook_bucket_alt =22
D/ContactsHelper: columName: mode =null
D/ContactsHelper: columName: data2 =Test
D/ContactsHelper: columName: group_sourceid =null
D/ContactsHelper: columName: starred =0
D/ContactsHelper: columName: photo_thumb_uri =content://com.android.contacts/contacts/9223372034707292162/photo
D/ContactsHelper: columName: times_used =null
D/ContactsHelper: columName: contact_status_icon =null
D/ContactsHelper: columName: contact_chat_capability =null
D/ContactsHelper: columName: account_name =PHONE
D/ContactsHelper: columName: sourceid =null

What I need is to get the associated phone number, but since the contact is not inside the contact provider or rawcontacts, I can not get it.

    
asked by Webserveis 18.09.2017 в 20:52
source

2 answers

1

Under construction ...

More or less I have found how to obtain them, but they need to be polished ...

public static SimpleContact TESTgetLocalProfile(Context context) {

    Uri PROFILE_URI = ContactsContract.Profile.CONTENT_URI;
    String[] PROJECTION = new String[]{
            ContactsContract.Profile._ID,
            ContactsContract.Profile.DISPLAY_NAME,
            ContactsContract.Profile.PHOTO_URI,
            ContactsContract.Profile.HAS_PHONE_NUMBER,
            ContactsContract.Profile.LOOKUP_KEY
    };

    Cursor c = context.getContentResolver().query(PROFILE_URI, PROJECTION, null, null, null);

    SimpleContact item = null;

    if (c != null && c.moveToFirst()) {
        String contactId = c.getString(c.getColumnIndex(ContactsContract.Profile._ID));
        item = new SimpleContact(contactId);
        item.setDisplayName(c.getString(c.getColumnIndex(ContactsContract.Profile.DISPLAY_NAME)));
        item.setPhoto(c.getString(c.getColumnIndex(ContactsContract.Profile.PHOTO_URI)));
        item.setLookupKey(c.getString(c.getColumnIndex(ContactsContract.Profile.LOOKUP_KEY)));

        if (c.getInt(c.getColumnIndex(ContactsContract.Profile.HAS_PHONE_NUMBER)) > 0) {

            Cursor pCur = context.getContentResolver().query(
                    // Retrieves data rows for the device user's 'profile' contact
                    Uri.withAppendedPath(
                            ContactsContract.Profile.CONTENT_URI,
                            ContactsContract.Contacts.Data.CONTENT_DIRECTORY),
                    new String[]{
                            ContactsContract.CommonDataKinds.Phone.NUMBER,
                            ContactsContract.CommonDataKinds.Phone.IS_PRIMARY,
                            ContactsContract.Contacts.Data.MIMETYPE
                    },

                    // Selects only email addresses or names
                    ContactsContract.Contacts.Data.MIMETYPE + "=? ",
                    new String[]{
                            ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
                    },

                    // Show primary rows first. Note that there won't be a primary email address if the
                    // user hasn't specified one.
                    ContactsContract.Contacts.Data.IS_PRIMARY + " DESC"
            );

            if (pCur != null && pCur.moveToFirst()) {
                item.setPhoneNumber(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                pCur.close();
            }

        }
        c.close();
    }

    return item;

}

Code adapted from AccountUtils.java

    
answered by 18.09.2017 / 23:30
source
1

By means of ContactsContract there is no way to obtain the customer data, the owner of the device usually adds his gmail account, but regularly not his phone in the contacts, in addition there is no indicator to determine which contact is the owner.

As an option to obtain the owner's account (s):

final AccountManager manager = AccountManager.get(this);
final Account[] accounts = manager.getAccountsByType("com.google");

String[] emails = new String[accounts.length];
for (int i = 0; i < accounts.length; i++) {
    emails[i] = accounts[i].name;
}
    
answered by 18.09.2017 в 21:38