Possible to get “Owner” contact info in Android?

我只是一个虾纸丫 提交于 2019-11-26 22:59:09

问题


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?


回答1:


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!




回答2:


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




回答3:


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();
            }
        }
    }
}



回答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();



回答5:


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()


来源:https://stackoverflow.com/questions/6222384/possible-to-get-owner-contact-info-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!