Can I get the "Owner" contact information in Android?

I could not find a direct answer to this. Can someone tell me if it is possible to get the contact information of the owner of the phone in the Android application?

+8
source share
5 answers

So the answer is technically no. The only way I have found so far to get information about the owner is through the account manager. Here is 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; } 

See http://code.google.com/p/google-api-java-client/wiki/AndroidAccountManager for details

+11
source

I found a very easy way (get it from copying to 4.1 messaging app)

projection for cursor

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

Cursor:

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

now just do simple

 cursor.moveToFirst(): 

and then enter the contact id through

 cursor.getString(0) 

and contact name through

 cursor.getString(1) 

and ..... everything is ready!

+13
source

What should we do:

1) Get the name of the user synchronization account (usually this is google email)
2) Get the contact from the contact book using this email
3) Get the contact details of this contact

Even close to perfection and requires two additional permissions - but at least it works.

Here is 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 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(); } } } } 
+6
source

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(); 
+3
source

Starting with API 23 and above, you need to add the correct permission to the manifest,

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

Then you can get information about the user, 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() 
0
source

All Articles