9

I haven't been able to find a straight answer on this. Can anyone tell me if it's possible to get the contact info of the phone's owner in an Android App?

wajiw
  • 12,239
  • 17
  • 54
  • 73
  • Do you mean the phone number? If so, you can use getLine1Number (http://developer.android.com/reference/android/telephony/TelephonyManager.html#getLine1Number%28%29) although it's not 100% reliable. – keyboardP Jun 03 '11 at 02:30
  • I'm looking for name and address – wajiw Jun 03 '11 at 02:33
  • i was looking into this too, my app involves the user entering in their name, email, and phone number, which are all obviously stored in the phone already. i cant find the pages i was reading but everything said that all of the methods of getting that info were/are being deprecated and taken out of the newer apis for security reasons. you could always just ask the user for it and save the data in `SharedPreferences` – dylan murphy Jun 03 '11 at 02:53

5 Answers5

13

I have found a very easy way (got it from digging into the 4.1 Messaging app!)

projection for cursor is

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

Cursor is :

Cursor cursor = activity.getContentResolver().query(Profile.CONTENT_URI, SELF_PROJECTION, null, null, null);

now just do a simple

cursor.moveToFirst():

and then fetch the contact id via

cursor.getString(0)

and the contact name via

cursor.getString(1)

and..... you're done!

Satheesh
  • 10,998
  • 6
  • 50
  • 93
Daksh
  • 1,177
  • 2
  • 18
  • 28
  • 1
    This method is only supported in Android 4.0 (API 14) or later. So if you only care about using this on 20-25% of devices, then it can work for you. – DanO Nov 08 '12 at 16:22
  • 1
    @Daniel, your comment was broadly true at the time you wrote it - but it's only gonna get more popular. And Daksh's code works a treat, as long as you surround it with `android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH`. Enough search queries hone in on this response that we should promote it for the future! – Kenton Price Dec 08 '12 at 03:49
  • Note that you'll need to add `` to your `AndroidManifest.xml` lest you get hit by `SecurityException` – declension Dec 22 '13 at 12:05
  • Is Possible to update “Owner” contact info in Android? – neeraj kirola Dec 18 '15 at 05:59
  • This needs the right READ_CONTACTS. Since apps requiring this permission are dubios, this isn't a good solution. Actually, the right to read ALL contacts must not be necessary in order to read the name only of the OWNER of the phone. – Holger Jakobs Feb 21 '16 at 22:24
  • Do I understand correctly that the Phone._ID field will be equivalent to the ContactsContract.Contacts.CONTACT_ID for the profile contact? – Josh Hansen Jun 26 '18 at 00:37
12

So the answer is technically no. The only way I've found so far to get owner's data is through the account manager. Here's an example of how to use it:

final AccountManager manager = AccountManager.get(this);
final Account[] accounts = manager.getAccountsByType("com.google");
final int size = accounts.length;
String[] names = new String[size];
for (int i = 0; i < size; i++) {
  names[i] = accounts[i].name;
}

For more info see: http://code.google.com/p/google-api-java-client/wiki/AndroidAccountManager

hichris123
  • 10,145
  • 15
  • 56
  • 70
wajiw
  • 12,239
  • 17
  • 54
  • 73
6

What we have to do:

1) Get user synchronization account name (which is usually google email)
2) Get contact from contact book with this email
3) Get contact data from this contact

Not even close to perfect, and needs two additional permissions - but at least works.

Here's the code, possible code updates can be here: https://gist.github.com/3904299

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;

public class OwnerInfo {
    // this class allows to get device information. It's done in two steps:
    // 1) get synchronization account email
    // 2) get contact data, associated with this email
    // by https://github.com/jehy
    //WARNING! You need to have permissions
    //
    //<uses-permission android:name="android.permission.READ_CONTACTS" />
    //<uses-permission android:name="android.permission.GET_ACCOUNTS" />
    //
    // in your AndroidManifest.xml for this code.

    public String id = null;
    public String email = null;
    public String phone = null;
    public String accountName = null;
    public String name = null;

    public OwnerInfo(Activity MainActivity) {
        final AccountManager manager = AccountManager.get(MainActivity);
        final Account[] accounts = manager.getAccountsByType("com.google");
        if (accounts[0].name != null) {
            accountName = accounts[0].name;

            ContentResolver cr = MainActivity.getContentResolver();
            Cursor emailCur = cr.query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Email.DATA + " = ?",
                    new String[] { accountName }, null);
            while (emailCur.moveToNext()) {
                id = emailCur
                        .getString(emailCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID));
                email = emailCur
                        .getString(emailCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                String newName = emailCur
                        .getString(emailCur
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                if (name == null || newName.length() > name.length())
                    name = newName;

                Log.v("Got contacts", "ID " + id + " Email : " + email
                        + " Name : " + name);
            }

            emailCur.close();
            if (id != null) {

                // get the phone number
                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[] { id }, null);
                while (pCur.moveToNext()) {
                    phone = pCur
                            .getString(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Log.v("Got contacts", "phone" + phone);
                }
                pCur.close();
            }
        }
    }
}
Jehy
  • 4,729
  • 1
  • 38
  • 55
  • It is useful to get profile info from table. Is it possible to update profile info? – neeraj kirola Dec 16 '15 at 09:30
  • @neerajkirola If you mean updating user's contacts - yes, it it possible if you have the permission to manage contacts. It is discussed in other questions, for example http://stackoverflow.com/questions/10603187/modify-native-contact-programmatically – Jehy Dec 17 '15 at 08:24
  • Is Possible to update “Owner” contact info in Android? – neeraj kirola Dec 18 '15 at 06:00
  • @neerajkirola it is just another contact in user's contacts, why not. – Jehy Dec 18 '15 at 08:47
  • Thanks for continuous replying my query...could u pls put that code here to update owner profile (name, contact number etc.) Thanks a lot !! – neeraj kirola Dec 21 '15 at 05:46
  • @neerajkirola This is offtopic, but you can check this code: http://stackoverflow.com/questions/9907751/android-update-a-contact – Jehy Dec 21 '15 at 07:20
4

For Ice Cream Sandwich or later, using

String[] columnNames = new String[] {Profile.DISPLAY_NAME, Profile.PHOTO_ID};

Cursor c = activity.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++) {
        String name = c.getString(0));
        long photoId = c.getLong(1));
    }
}
c.close();
Bao Le
  • 16,643
  • 9
  • 65
  • 68
1

From API 23 and above you need to add proper permission in manifest,

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

Then you will be able to retrieve user info like,

String[] columnNames = new String[] {ContactsContract.Profile.DISPLAY_NAME, ContactsContract.Profile.PHOTO_ID};

Cursor c = activity.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 < count; j++) {
        String name = c.getString(c.getColumnIndex(ContactsContract.Profile.DISPLAY_NAME));
        String photoID = c.getString(c.getColumnIndex(ContactsContract.Profile.PHOTO_ID));
        Log.i("MainActivity", "name: " + name);
        Log.i("MainActivity", "photoID: "+ photoID);
    }
}
c.close()
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256